-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLCA.py
More file actions
29 lines (25 loc) · 718 Bytes
/
Copy pathLCA.py
File metadata and controls
29 lines (25 loc) · 718 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
26
27
# coding:utf-8
__author__ = 'devin'
# 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
"""
m = root
while m is not None:
if p.val <= m.val <= q.val or q.val <= m.x <= p.val:
break
elif p.val > m.val and q.val > m.val:
m = m.right
elif p.val < m.val and q.val < m.val:
m = m.left
return m