题目描述
blablabla
样例
# 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 not root:
return None
if q == root or p == root: # 如果找到了某一个点 则当前节点为答案
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if left and right: # 如果左子树中可以找到其公共祖先 右子树也可以 则说明当前节点就是答案
return root
if not left: # 左边找不到了 那就是右子树的答案
return right
elif not right: # 右边亦然
return left
请问不用考虑树中有多个与输入节点相同的结点是吗?