forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlowest-common-ancestor-of-a-binary-tree-iv.py
57 lines (50 loc) · 1.65 KB
/
lowest-common-ancestor-of-a-binary-tree-iv.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
# Time: O(n)
# Space: O(h)
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
pass
class Solution(object):
def lowestCommonAncestor(self, root, nodes):
"""
:type root: TreeNode
:type nodes: List[TreeNode]
"""
def iter_dfs(root, lookup):
result = [0]
stk = [(1, (root, result))]
while stk:
step, args = stk.pop()
if step == 1:
node, ret = args
if not node or node in lookup:
ret[0] = node
continue
ret1, ret2 = [None], [None]
stk.append((2, (node, ret1, ret2, ret)))
stk.append((1, (node.right, ret2)))
stk.append((1, (node.left, ret1)))
elif step == 2:
node, ret1, ret2, ret = args
if ret1[0] and ret2[0]:
ret[0] = node
else:
ret[0] = ret1[0] or ret2[0]
return result[0]
return iter_dfs(root, set(nodes))
# Time: O(n)
# Space: O(h)
class Solution2(object):
def lowestCommonAncestor(self, root, nodes):
"""
:type root: TreeNode
:type nodes: List[TreeNode]
"""
def dfs(node, lookup):
if not node or node in lookup:
return node
left, right = dfs(node.left, lookup), dfs(node.right, lookup)
if left and right:
return node
return left or right
return dfs(root, set(nodes))