-
Notifications
You must be signed in to change notification settings - Fork 0
/
109_sortedListToBST.txt
40 lines (40 loc) · 1.07 KB
/
109_sortedListToBST.txt
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
/**
* 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:
TreeNode* sortedListToBST(ListNode* head) {
//根据中序遍历还原二叉搜索树
if(head==nullptr)
return nullptr;
if(head->next==nullptr)
return new TreeNode(head->val);
ListNode* slow = head,*fast = head,*pre=nullptr;
//快慢指针找到根结点
while(fast!=nullptr && fast->next!=nullptr){
pre = slow;
slow = slow->next;
fast = fast->next->next;
}
//将链表一分为二
pre->next = nullptr;
TreeNode* root = new TreeNode(slow->val);
root->left = sortedListToBST(head);
root->right = sortedListToBST(slow->next);
return root;
}
};