-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertSortedListToBinarySearchTree.cpp
More file actions
56 lines (51 loc) · 1.29 KB
/
ConvertSortedListToBinarySearchTree.cpp
File metadata and controls
56 lines (51 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/***
* 递归
* 先找到链表的中点,设为根
* 然后分别处理左右两部分
* 找中点,可以用快慢指针,一个走一步,一个走两步
***/
class Solution {
public:
TreeNode *sortedListToBST(ListNode *head) {
if (NULL == head)
return NULL;
// find mid of list
ListNode *prenode = NULL; // be used when break the list
ListNode *slow = head;
ListNode *fast = head;
while (fast && fast->next)
{
prenode = slow;
slow = slow->next;
fast = fast->next->next;
}
// new a TreeNode, and process the children recursivly
TreeNode *root = new TreeNode(slow->val);
if (prenode)
{
prenode->next = NULL; // break the list
root->left = sortedListToBST(head);
}
else
root->left = NULL;
root->right = sortedListToBST(slow->next);
return root;
}
};