Ejemplo n.º 1
0
            return False
        leftres = self.lowestCommonAncestor(root.left, p, q)
        rightres = self.lowestCommonAncestor(root.right, p, q)
        if isinstance(leftres, TreeNode):  # 左子树已全部找到
            return leftres
        elif isinstance(rightres, TreeNode):  # 右子树已全部找到
            return rightres
        elif leftres or rightres:   # 有1/2个找到
            if root.val == p.val or root.val == q.val or (leftres and rightres):
                return root
            else:
                return True
        else:                      # 子树均没有找到
            if root.val == p.val or root.val == q.val:  # 根找到一个
                return True
            else:
                return False


from Debug_BST import list2Tree, PrintTree

sol = Solution()
data = [3, 5, 1, 6, 2, 0, 8, None, None, 7, 4]

tree = list2Tree(data)
printT = PrintTree()
printT.printTree(tree)

res = sol.lowestCommonAncestor(tree, TreeNode(5), TreeNode(2))
print(res.val)