-
Notifications
You must be signed in to change notification settings - Fork 0
/
236.py
52 lines (39 loc) · 1.3 KB
/
236.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
class TreeNode(object):
def __init__(self, v):
self.val = v
self.left = None
self.right = None
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
if root is None or root is p or root is q:
return root
left_common = self.lowestCommonAncestor(root.left, p, q)
right_common = self.lowestCommonAncestor(root.right, p, q)
if left_common is None and right_common is None:
return None
elif left_common is None and right_common is not None:
return right_common
elif left_common is not None and right_common is None:
return left_common
else:
if left_common != right_common:
return root
else:
return right_common
if __name__ == "__main__":
r = TreeNode(-1)
r.left = TreeNode(0)
r.left.left = TreeNode(-2)
r.left.left.left = TreeNode(8)
r.left.right = TreeNode(4)
r.right = TreeNode(3)
print Solution().lowestCommonAncestor(r, r.left.left.left, r.left.right).val