def inorder(root):
if not root: return
inorder(root.left)
nodes.append(root.val)
inorder(root.right)
nodes = []
inorder(root)
return min([abs(nodes[i] - nodes[i+1]) for i in range(len(nodes)-1)])
LeetCode.510 二叉搜索树中的众数
哈希表
def inorder(root):
if not root: return
nodes[root.val] = nodes.get(root.val, 0) + 1
inorder(root.left)
inorder(root.right)
nodes = {}
inorder(root)
maxval = max(nodes.values())
return [i for i in nodes if nodes[i] == maxval]
LeetCode.236 二叉树的最近公共祖先
if not root: return
if root.val == q.val or root.val == p.val: return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if left and not right: return left
if right and not left: return right
if not left and not right: return
return root