forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clone-n-ary-tree.py
52 lines (46 loc) · 1.31 KB
/
clone-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
# 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 []
class Solution(object):
def cloneTree(self, root):
"""
:type root: Node
:rtype: Node
"""
result = [None]
stk = [(1, (root, result))]
while stk:
step, params = stk.pop()
if step == 1:
node, ret = params
if not node:
continue
ret[0] = Node(node.val)
for child in reversed(node.children):
ret1 = [None]
stk.append((2, (ret1, ret)))
stk.append((1, (child, ret1)))
else:
ret1, ret = params
ret[0].children.append(ret1[0])
return result[0]
# Time: O(n)
# Space: O(h)
class Solution2(object):
def cloneTree(self, root):
"""
:type root: Node
:rtype: Node
"""
def dfs(node):
if not node:
return None
copy = Node(node.val)
for child in node.children:
copy.children.append(dfs(child))
return copy
return dfs(root)