-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCAofBST.py
More file actions
26 lines (24 loc) · 780 Bytes
/
Copy pathLCAofBST.py
File metadata and controls
26 lines (24 loc) · 780 Bytes
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
# 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
while (root.val - p.val) * (root.val - q.val) > 0:
root = (root.left, root.right)[p.val > root.val]
return root
# if p.val < root.val > q.val:
# return self.lowestCommonAncestor(root.left, p, q)
# if p.val > root.val < q.val:
# return self.lowestCommonAncestor(root.right, p, q)
# return root