class Solution:
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
res=[0]
if p.val>q.val: p,q=q,p
flag=[False]
def DFS(root,p,q):
if p.val<=root.val<=q.val:
res[0]=root
return
elif root.val<p.val:
DFS(root.right,p,q)
elif root.val>q.val:
DFS(root.left,p,q)
DFS(root,p,q)
return res[0]
python leetcode 235. Lowest Common Ancestor of a Binary Search Tree
最新推荐文章于 2022-02-13 02:31:00 发布