牛客这里题目经常不明确,节点和节点值应当明显区分一下
给的输入例子,也有时候明明是字符串型'1',输入中显示给的却是 1
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
#
# @param root TreeNode类
# @param o1 int整型
# @param o2 int整型
# @return int整型
#
class Solution:
def lowestCommonAncestor(self , root , o1 , o2 ):
# write code here
def haschild(root, o1, o2):
if root is None or self.ancestor is not None:
return False
ans = root.val == o1 or root.val == o2
lson = haschild(root.left, o1, o2)
rson = haschild(root.right, o1, o2)
if (lson and rson) or (ans and (lson or rson)):
self.ancestor = root.val
return
return ans or lson or rson
self.ancestor = None
haschild(root, o1, o2)
return self.ancestor
京公网安备 11010502036488号