-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path109.cpp
47 lines (45 loc) · 1.05 KB
/
109.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
ListNode *node;
// 利用递归的规律
TreeNode* solute(int l, int r) {
if(l > r) return NULL;
int m = (l + r) / 2;
TreeNode* left = solute(l, m - 1);
TreeNode* root = new TreeNode(node->val);
node = node->next;
TreeNode* right = solute(m + 1, r);
root->left = left;
root->right = right;
return root;
}
TreeNode* sortedListToBST(ListNode* head) {
if(head == NULL) return NULL;
int len = 0;
ListNode *tem = head;
node = head;
while(tem != NULL)
{
tem = tem->next;
len ++;
}
return solute(0, len-1);
}
};