To prove safety OFFER ---- 68-2, the most recent common ancestor binary tree (js achieve)

Topic
Given a binary tree, find the nearest common ancestor of the two specified nodes of the tree.

Baidu Encyclopedia recent common ancestor as defined: "For two nodes of the root of the tree T p, q, is represented as a common ancestor node x, that x is p, q ancestor depth as large as possible and x (a node can be its own ancestors). "

For example, a given binary tree as follows: root = [3,5,1,6,2,0,8, null, null, 7,4]
Here Insert Picture Description

Input: root = [3,5,1,6,2,0,8, null, null, 7,4], p = 5, q = 1
Output: 3
Explanation: node 5 and node 1 is a common ancestor node 3.

Input: root = [3,5,1,6,2,0,8, null, null, 7,4], p = 5, q = 4
Output: 5
Explanation: node 5 and node 4 is the common ancestor node 5. Because by definition a recent common ancestor node can be a node itself.


Thinking


Guess you like

Origin blog.csdn.net/qq_40816360/article/details/95236503