-
Notifications
You must be signed in to change notification settings - Fork 0
/
lca.py
52 lines (46 loc) · 1.48 KB
/
lca.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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# 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):
return None
if (root.val == p.val):
if ( self.findKey(root.left, q) or self.findKey(root.right, q) ):
return root
else:
return None
elif (root.val == q.val):
if ( self.findKey(root.left, p) or self.findKey(root.right, p) ):
return root
else:
return None
left = None
right = None
if (root.val > p.val and root.val > q.val):
return self.lowestCommonAncestor(root.left, p, q)
elif (root.val < p.val and root.val < q.val):
return self.lowestCommonAncestor(root.right, p, q)
else:
if (self.findKey(root, p) and self.findKey(root, q)):
return root
else:
return None
def findKey(self, root, key):
if root is None:
return None
if root.val == key.val:
return True
if key.val < root.val:
return self.findKey(root.left, key)
else:
return self.findKey(root.right, key)