-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-sorted-list-to-binary-search-tree.cpp
52 lines (47 loc) · 1.23 KB
/
convert-sorted-list-to-binary-search-tree.cpp
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
ListNode *list;
int count(ListNode *node){
int size=0;
while(node){
size++;
node=node->next;
}
return size;
}
TreeNode* generate(int n){
if(n==0)
return NULL;
TreeNode* root = new TreeNode(0);
root->left=generate(n/2);
root->val=list->val;
list=list->next;
root->right=generate(n-n/2-1);
return root;
}
TreeNode* sortedListToBST(ListNode* head) {
this->list=head;
return generate(count(head));
}
};