-
Notifications
You must be signed in to change notification settings - Fork 43
/
split-bst.py
247 lines (223 loc) · 7.27 KB
/
split-bst.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""
LC 776 - Split BST
Problem Description
Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the target value. It’s not necessarily the case that the tree contains a node with value V.
Additionally, most of the structure of the original tree should remain. Formally, for any child C with parent P in the original tree, if they are both in the same subtree after the split, then node C should still have the parent P.
You should output the root TreeNode of both subtrees after splitting, in any order.
Example 1:
Input: root = [4,2,6,1,3,5,7], V = 2
Output: [[2,1],[4,3,6,null,null,5,7]]
Explanation:
Note that root, output[0], and output[1] are TreeNode objects, not arrays.
The given tree [4,2,6,1,3,5,7] is represented by the following diagram:
4
/ \
2 6
/ \ / \
1 3 5 7
while the diagrams for the outputs are:
4
/ \
3 6 and 2
/ \ /
5 7 1
Note:
The size of the BST will not exceed 50.
The BST is always valid and each node’s value is different.
"""
# V0
# IDEA : BST properties (left < root < right) + recursion
# https://blog.csdn.net/magicbean2/article/details/79679927
# https://www.itdaan.com/tw/d58594b92742689b5769f9827365e8b4
### STEPS
# -> 1) check whether root.val > or < V
# -> if root.val > V :
# - NO NEED TO MODIFY ALL RIGHT SUB TREE
# - BUT NEED TO re-connect nodes in LEFT SUB TREE WHICH IS BIGGER THAN V (root.left = right)
# -> if root.val < V :
# - NO NEED TO MODIFY ALL LEFT SUB TREE
# - BUT NEED TO re-connect nodes in RIGHT SUB TREE WHICH IS SMALLER THAN V (root.right = left)
# -> 2) return result
class Solution(object):
def splitBST(self, root, V):
if not root: return [None, None]
### NOTE : if root.val <= V
if root.val > V:
left, right = self.splitBST(root.left, V)
root.left = right
return [left, root]
### NOTE : if root.val > V
else:
left, right = self.splitBST(root.right, V)
root.right = left
return [root, right]
# V0'
# IDEA : BST properties (left < root < right) + recursion
class Solution(object):
def splitBST(self, root, V):
if not root:
return None, None
### NOTE : if root.val <= V
elif root.val <= V:
result = self.splitBST(root.right, V)
root.right = result[0]
return root, result[1]
### NOTE : if root.val > V
else:
result = self.splitBST(root.left, V)
root.left = result[1]
return result[0], root
# V0'
class Solution(object):
def splitBST(self, root, V):
if not root:
return None, None
elif root.val <= V:
result = self.splitBST(root.right, V)
root.right = result[0]
return root, result[1]
elif root.val > V:
result = self.splitBST(root.left, V)
root.left = result[1]
return result[0], root
# V0''
class Solution(object):
def splitBST(self, root, V):
if not root: return [None, None]
if root.val > V:
left, right = self.splitBST(root.left, V)
root.left = right
return [left, root]
left, right = self.splitBST(root.right, V)
root.right = left
return [root, right]
# V1
# http://bookshadow.com/weblog/2018/02/04/leetcode-split-bst/
# https://blog.csdn.net/magicbean2/article/details/79679927
# https://www.itdaan.com/tw/d58594b92742689b5769f9827365e8b4
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def splitBST(self, root, V):
"""
:type root: TreeNode
:type V: int
:rtype: List[TreeNode]
"""
if not root: return [None, None]
if root.val > V:
left, right = self.splitBST(root.left, V)
root.left = right
return [left, root]
left, right = self.splitBST(root.right, V)
root.right = left
return [root, right]
### Test case : dev
# V1'
# https://blog.csdn.net/magicbean2/article/details/79679927
# JAVA
# /**
# * 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:
# vector<TreeNode*> splitBST(TreeNode* root, int V) {
# vector<TreeNode *> res(2, NULL);
# if(root == NULL) {
# return res;
# }
# if(root->val > V) { // the right child is retained
# res[1] = root;
# auto res1 = splitBST(root->left, V);
# root->left = res1[1];
# res[0]=res1[0];
# }
# else { // the left child is retained
# res[0] = root;
# auto res1 = splitBST(root->right, V);
# root->right = res1[0];
# res[1] = res1[1];
# }
# return res;
# }
# };
# V1''
# https://www.itdaan.com/tw/d58594b92742689b5769f9827365e8b4
# C++
# class Solution {
# public:
# vector<TreeNode*> splitBST(TreeNode* root, int V) {
# vector<TreeNode*> res{NULL, NULL};
# if (!root) return res;
# if (root->val <= V) {
# res = splitBST(root->right, V);
# root->right = res[0];
# res[0] = root;
# } else {
# res = splitBST(root->left, V);
# root->left = res[1];
# res[1] = root;
# }
# return res;
# }
# };
# V1'''
# https://www.acwing.com/solution/LeetCode/content/208/
# IDEA : JAVA
# class Solution {
# public TreeNode[] splitBST(TreeNode root, int V) {
# TreeNode[] ans = new TreeNode[2];
# return dfs(root, V);
# }
# public TreeNode[] dfs(TreeNode node, int v) {
# TreeNode[] ans = new TreeNode[2];
# if(node==null) return ans;
# if(node.val>v) {
# ans[1] = node;
# TreeNode left = node.left;
# node.left = null;
# TreeNode[] nodes = dfs(left, v);
# node.left = nodes[1];
# ans[0] = nodes[0];
# }else {
# ans[0] = node;
# TreeNode right = node.right;
# node.right = null;
# TreeNode[] nodes = dfs(right, v);
# node.right = nodes[0];
# ans[1]=nodes[1];
# }
# return ans;
# }
#
# }
# V2
# Time: O(n)
# Space: O(h)
class Solution(object):
def splitBST(self, root, V):
"""
:type root: TreeNode
:type V: int
:rtype: List[TreeNode]
"""
if not root:
return None, None
elif root.val <= V:
result = self.splitBST(root.right, V)
root.right = result[0]
return root, result[1]
else:
result = self.splitBST(root.left, V)
root.left = result[1]
return result[0], root