-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-sorted-list-to-binary-search-tree.cc
94 lines (74 loc) · 1.84 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
#include <iostream>
using namespace std;
// 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) {}
};
#if 1
// http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html
#else
ListNode *middleNode(ListNode *head, ListNode *tail) {
if (head == NULL || head == tail || head->next == tail) {
return head;
}
ListNode *fast = head, *slow = head;
while (fast->next != tail && fast->next->next != tail) {
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
// 为了不破换单链表的结构,传入链表的结尾
TreeNode *sortedListToBST(ListNode *head, ListNode *tail) {
if (head == NULL) {
return NULL;
}
if (head->next == tail) {
return new TreeNode(head->val);
}
ListNode *mid = middleNode(head, tail);
ListNode *left = head, *right = mid->next;
TreeNode *root = new TreeNode(mid->val);
if (left != mid) {
root->left = sortedListToBST(left, mid);
}
root->right = sortedListToBST(right, tail);
return root;
}
TreeNode *sortedListToBST(ListNode *head) {
return sortedListToBST(head, NULL);
}
#endif
void inorder(TreeNode *r) {
if (r) {
inorder(r->left);
cout << r->val << endl;
inorder(r->right);
}
}
// http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html
int main(int argc, char const* argv[])
{
ListNode *n1 = new ListNode(-10);
ListNode *n2 = new ListNode(-3);
ListNode *n3 = new ListNode(0);
ListNode *n4 = new ListNode(5);
ListNode *n5 = new ListNode(9);
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
TreeNode *r = sortedListToBST(n1);
inorder(r);
return 0;
}