L474 Lowest Common Ancestor II
Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes.
The lowest common ancestor is the node with largest depth which is the ancestor of both nodes.
The node has an extra attributeparent
which point to the father of itself. The root's parent is null.
Example
For the following binary tree:
LCA(3, 5) =4
LCA(5, 6) =7
LCA(6, 7) =7
从点开始向上遍历,把从两个点到根的路径分别存起来。然后找intersection,相交的那个点就是LCA。
Last updated