-
Notifications
You must be signed in to change notification settings - Fork 160
/
Convert_Sorted_List_to_Binary_Search_Tree.cc
51 lines (51 loc) · 1.28 KB
/
Convert_Sorted_List_to_Binary_Search_Tree.cc
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
/**
* 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:
int GetListLen(ListNode *pt) {
int len = 0;
while (pt != NULL) {
len++;
pt = pt->next;
}
return len;
}
TreeNode *GenTree(ListNode *pCnt, int l, int r) {
if (l > r) {
return NULL;
}
int mid = l + (r - l) / 2;
ListNode *pt = pCnt;
for (int i = l; i < mid; i++) {
pt = pt->next;
}
TreeNode *root = new TreeNode(pt->val);
root->left = GenTree(pCnt, l, mid - 1);
root->right = GenTree(pt->next, mid + 1, r);
return root;
}
TreeNode *sortedListToBST(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (head == NULL) {
return NULL;
}
int len = GetListLen(head);
return GenTree(head, 0, len - 1);
}
};