forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
move-sub-tree-of-n-ary-tree.py
199 lines (184 loc) · 6.17 KB
/
move-sub-tree-of-n-ary-tree.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
# Time: O(n)
# Space: O(h)
# Definition for a Node.
class Node(object):
def __init__(self, val=None, children=None):
self.val = val
self.children = children if children is not None else []
# one pass solution without recursion
class Solution(object):
def moveSubTree(self, root, p, q):
"""
:type root: Node
:type p: Node
:type q: Node
:rtype: Node
"""
def iter_find_parents(node, parent, p, q, is_ancestor, lookup):
stk = [(1, [node, None, False])]
while stk:
step, params = stk.pop()
if step == 1:
node, parent, is_ancestor = params
if node in (p, q):
lookup[node] = parent
if len(lookup) == 2:
return is_ancestor
stk.append((2, [node, is_ancestor, reversed(node.children)]))
else:
node, is_ancestor, it = params
child = next(it, None)
if not child:
continue
stk.append((2, [node, is_ancestor, it]))
stk.append((1, [child, node, is_ancestor or node == p]))
assert(False)
return False
lookup = {}
is_ancestor = iter_find_parents(root, None, p, q, False, lookup)
if p in lookup and lookup[p] == q:
return root
q.children.append(p)
if not is_ancestor:
lookup[p].children.remove(p)
else:
lookup[q].children.remove(q)
if p == root:
root = q
else:
lookup[p].children[lookup[p].children.index(p)] = q
return root
# Time: O(n)
# Space: O(h)
# one pass solution with recursion (bad in deep tree)
class Solution_Recu(object):
def moveSubTree(self, root, p, q):
"""
:type root: Node
:type p: Node
:type q: Node
:rtype: Node
"""
def find_parents(node, parent, p, q, is_ancestor, lookup):
if node in (p, q):
lookup[node] = parent
if len(lookup) == 2:
return True, is_ancestor
for child in node.children:
found, new_is_ancestor = find_parents(child, node, p, q, is_ancestor or node == p, lookup)
if found:
return True, new_is_ancestor
return False, False
lookup = {}
is_ancestor = find_parents(root, None, p, q, False, lookup)[1]
if p in lookup and lookup[p] == q:
return root
q.children.append(p)
if not is_ancestor:
lookup[p].children.remove(p)
else:
lookup[q].children.remove(q)
if p == root:
root = q
else:
lookup[p].children[lookup[p].children.index(p)] = q
return root
# Time: O(n)
# Space: O(h)
# two pass solution without recursion
class Solution2(object):
def moveSubTree(self, root, p, q):
"""
:type root: Node
:type p: Node
:type q: Node
:rtype: Node
"""
def iter_find_parents(node, parent, p, q, lookup):
stk = [(1, [node, None])]
while stk:
step, params = stk.pop()
if step == 1:
node, parent = params
if node in (p, q):
lookup[node] = parent
if len(lookup) == 2:
return
stk.append((2, [node, reversed(node.children)]))
else:
node, it = params
child = next(it, None)
if not child:
continue
stk.append((2, [node, it]))
stk.append((1, [child, node]))
def iter_is_ancestor(node, q):
stk = [(1, [node])]
while stk:
step, params = stk.pop()
if step == 1:
node = params[0]
stk.append((2, [reversed(node.children)]))
else:
it = params[0]
child = next(it, None)
if not child:
continue
if child == q:
return True
stk.append((2, [it]))
stk.append((1, [child]))
return False
lookup = {}
iter_find_parents(root, None, p, q, lookup)
if p in lookup and lookup[p] == q:
return root
q.children.append(p)
if not iter_is_ancestor(p, q):
lookup[p].children.remove(p)
else:
lookup[q].children.remove(q)
if p == root:
root = q
else:
lookup[p].children[lookup[p].children.index(p)] = q
return root
# Time: O(n)
# Space: O(h)
# two pass solution with recursion (bad in deep tree)
class Solution2_Recu(object):
def moveSubTree(self, root, p, q):
"""
:type root: Node
:type p: Node
:type q: Node
:rtype: Node
"""
def find_parents(node, parent, p, q, lookup):
if node in (p, q):
lookup[node] = parent
if len(lookup) == 2:
return True
for child in node.children:
if find_parents(child, node, p, q, lookup):
return True
return False
def is_ancestor(node, q):
for child in node.children:
if node == q or is_ancestor(child, q):
return True
return False
lookup = {}
find_parents(root, None, p, q, lookup)
if p in lookup and lookup[p] == q:
return root
q.children.append(p)
if not is_ancestor(p, q):
lookup[p].children.remove(p)
else:
lookup[q].children.remove(q)
if p == root:
root = q
else:
lookup[p].children[lookup[p].children.index(p)] = q
return root