mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-13 19:31:42 +08:00
add leetcode problem-cn part2
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<p>Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree.</p>
|
||||
|
||||
<p>For example, Given the following tree: root = [3,5,1,6,2,0,8,null,null,7,4]</p>
|
||||
|
||||
<pre>
|
||||
3
|
||||
/ \
|
||||
5 1
|
||||
/ \ / \
|
||||
6 2 0 8
|
||||
/ \
|
||||
7 4
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
|
||||
<strong>Input:</strong> 3
|
||||
<strong>Explanation:</strong> The first common ancestor of node 5 and node 1 is node 3.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The first common ancestor of node 5 and node 4 is node 5.</pre>
|
||||
|
||||
<p><strong>Notes:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>All node values are pairwise distinct.</li>
|
||||
<li>p, q are different node and both can be found in the given tree.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user