-
Notifications
You must be signed in to change notification settings - Fork 0
/
JZ86.py
38 lines (34 loc) · 1.11 KB
/
JZ86.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
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param root TreeNode类
# @param o1 int整型
# @param o2 int整型
# @return int整型
#
class Solution:
def lowestCommonAncestor(self, root: TreeNode, o1: int, o2: int) -> int:
self.ancestor = None
def bottomUp(node: TreeNode, val1: int, val2: int) -> bool:
if node is None or self.ancestor is not None:
return False
l = bottomUp(node.left, val1, val2)
r = bottomUp(node.right, val1, val2)
if l and r:
self.ancestor = node
return False
elif not l and not r:
return node.val == val1 or node.val == val2
elif l or r:
if node.val == val1 or node.val == val2:
self.ancestor = node
return False
return True
bottomUp(root, o1, o2)
return self.ancestor.val