1.Description
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”
2.Example
3.Code
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
dic={root:None}
def bfs(node):
if node:
if node.left:dic[node.left]=node
if node.right:dic[node.right]=node
bfs(node.left)
bfs(node.right)
bfs(root)
l1,l2=p,q
while(l1!=l2):
l1=dic.get(l1) if l1 else q
l2=dic.get(l2) if l2 else p
return l1
4.Result