forked from keineahnung2345/leetcode-cpp-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
109. Convert Sorted List to Binary Search Tree.cpp
188 lines (146 loc) · 5.77 KB
/
109. 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//recursion
//Runtime: 40 ms, faster than 46.56% of C++ online submissions for Convert Sorted List to Binary Search Tree.
//Memory Usage: 36.1 MB, less than 5.43% of C++ online submissions for Convert Sorted List to Binary Search Tree.
/**
* 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:
TreeNode* sortedListToBST(ListNode* head) {
if(!head) return nullptr;
ListNode* dummy = new ListNode(-1);
dummy->next = head;
ListNode *slow = dummy, *fast = dummy;
ListNode *preslow;
while(fast && fast->next){
preslow = slow;
slow = slow->next;
fast = fast->next->next;
}
cout << preslow->val << ", " << slow->val << endl;
//now slow is the midpoint(for odd-length list) or
// the node before midpoint(for even-length list)
//preslow is the previous node of slow
TreeNode* root = new TreeNode(slow->val);
//cut the list's former part with the processed "slow"
preslow->next = nullptr;
//when slow==head, this node doesn't have left subtree
root->left = (slow == head) ? nullptr : sortedListToBST(head);
root->right = sortedListToBST(slow->next);
return root;
}
};
//recursion, without dummy
//Runtime: 40 ms, faster than 46.56% of C++ online submissions for Convert Sorted List to Binary Search Tree.
//Memory Usage: 32.9 MB, less than 5.43% of C++ online submissions for Convert Sorted List to Binary Search Tree.
//time: O(NlogN)
//in ith recursion(i starts from 1), we use (N/pow(2,i)) times to find middle point, do it pow(2,i-1) times
//and we will do that for logN recursions, so it sums up to NlogN/2, which is O(NlogN)
//space: O(logN)
//recursion space used by "balanced" tree
class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
if(!head) return nullptr;
ListNode *slow = head, *fast = head;
ListNode *preslow = nullptr;
while(fast && fast->next){
preslow = slow;
slow = slow->next;
fast = fast->next->next;
}
// cout << (preslow ? preslow->val : -1) << ", " << slow->val << endl;
//now slow is the midpoint(for odd-length list) or
// the node after midpoint(for even-length list)
//preslow is the previous node of slow
TreeNode* root = new TreeNode(slow->val);
//cut the list's former part with the processed "slow"
//if slow is head, it means the list's length is 1,
//and preslow is nullptr
if(slow == head){
return root;
}
//if slow != head, it has left subtree
preslow->next = nullptr;
root->left = sortedListToBST(head);
root->right = sortedListToBST(slow->next);
return root;
}
};
//Approach 2: Recursion + Conversion to Array
//use more space to reduce time complexity
//Runtime: 40 ms, faster than 46.56% of C++ online submissions for Convert Sorted List to Binary Search Tree.
//Memory Usage: 33.4 MB, less than 5.43% of C++ online submissions for Convert Sorted List to Binary Search Tree.
//time: O(N), space: O(N)
class Solution {
public:
TreeNode* sortedVectorToBST(vector<int>& arr, int start, int end){
if(start > end) return nullptr;
int mid = (start+end)>>1;
TreeNode* root = new TreeNode(arr[mid]);
root->left = sortedVectorToBST(arr, start, mid-1);
root->right = sortedVectorToBST(arr, mid+1, end);
return root;
}
TreeNode* sortedListToBST(ListNode* head) {
vector<int> arr;
while(head){
arr.push_back(head->val);
head = head->next;
}
int n = arr.size();
return sortedVectorToBST(arr, 0, n-1);
}
};
//Approach 3: Inorder Simulation
//Runtime: 40 ms, faster than 46.56% of C++ online submissions for Convert Sorted List to Binary Search Tree.
//Memory Usage: 33.1 MB, less than 5.43% of C++ online submissions for Convert Sorted List to Binary Search Tree.
//time: O(N), space: O(logN)
class Solution {
public:
ListNode* head;
TreeNode* convertListToBST(int start, int end){
if(start > end){
// cout << "return nullptr" << endl;
return nullptr;
}
int mid = (start+end) >> 1;
TreeNode* left = convertListToBST(start, mid-1);
// cout << start << ", " << end << ", " << this->head->val << endl;
TreeNode* root = new TreeNode(this->head->val);
root->left = left;
this->head = this->head->next;
root->right = convertListToBST(mid+1, end);
// cout << "return " << root->val << endl;
return root;
}
TreeNode* sortedListToBST(ListNode* head) {
if(!head) return nullptr;
int n = 0;
ListNode* cur = head;
while(cur){
++n;
cur = cur->next;
}
this->head = head;
return convertListToBST(0, n-1);
}
};