def lowestCommonAncestor(root, p, q):
if not root or root == p or root == q:
return root
left = lowestCommonAncestor(root.left, p, q)
right = lowestCommonAncestor(root.right, p, q)
if left and right:
return root
if left:
return left
if right:
return right
return None
236_二叉树的最近公共祖先
最新推荐文章于 2024-09-29 23:18:29 发布