|
1 |
| -# [04.08. First Common Ancestor](https://leetcode-cn.com/problems/first-common-ancestor-lcci) |
2 |
| - |
3 |
| -## Description |
4 |
| -<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> |
5 |
| -
|
6 |
| -<p>For example, Given the following tree: root = [3,5,1,6,2,0,8,null,null,7,4]</p> |
7 |
| -
|
8 |
| -<pre> |
9 |
| - 3 |
10 |
| - / \ |
11 |
| - 5 1 |
12 |
| - / \ / \ |
13 |
| -6 2 0 8 |
14 |
| - / \ |
15 |
| - 7 4 |
16 |
| -</pre> |
17 |
| -
|
18 |
| -<p><strong>Example 1:</strong></p> |
19 |
| -
|
20 |
| -<pre> |
21 |
| -<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 |
22 |
| -<strong>Input:</strong> 3 |
23 |
| -<strong>Explanation:</strong> The first common ancestor of node 5 and node 1 is node 3.</pre> |
24 |
| -
|
25 |
| -<p><strong>Example 2:</strong></p> |
26 |
| -
|
27 |
| -<pre> |
28 |
| -<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 |
29 |
| -<strong>Output:</strong> 5 |
30 |
| -<strong>Explanation:</strong> The first common ancestor of node 5 and node 4 is node 5.</pre> |
31 |
| -
|
32 |
| -<p><strong>Notes:</strong></p> |
33 |
| -
|
34 |
| -<ul> |
35 |
| - <li>All node values are pairwise distinct.</li> |
36 |
| - <li>p, q are different node and both can be found in the given tree.</li> |
37 |
| -</ul> |
38 |
| - |
39 |
| - |
40 |
| - |
41 |
| -## Solutions |
42 |
| - |
43 |
| - |
44 |
| -### Python3 |
45 |
| - |
46 |
| -```python |
47 |
| - |
48 |
| -``` |
49 |
| - |
50 |
| -### Java |
51 |
| - |
52 |
| -```java |
53 |
| - |
54 |
| -``` |
55 |
| - |
56 |
| -### ... |
57 |
| -``` |
58 |
| - |
59 |
| -``` |
| 1 | +# [04.08. First Common Ancestor](https://leetcode-cn.com/problems/first-common-ancestor-lcci) |
| 2 | + |
| 3 | +## Description |
| 4 | +<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> |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +<p>For example, Given the following tree: root = [3,5,1,6,2,0,8,null,null,7,4]</p> |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +<pre> |
| 13 | + |
| 14 | + 3 |
| 15 | + |
| 16 | + / \ |
| 17 | + |
| 18 | + 5 1 |
| 19 | + |
| 20 | + / \ / \ |
| 21 | + |
| 22 | +6 2 0 8 |
| 23 | + |
| 24 | + / \ |
| 25 | + |
| 26 | + 7 4 |
| 27 | + |
| 28 | +</pre> |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +<p><strong>Example 1:</strong></p> |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +<pre> |
| 37 | + |
| 38 | +<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 |
| 39 | + |
| 40 | +<strong>Input:</strong> 3 |
| 41 | + |
| 42 | +<strong>Explanation:</strong> The first common ancestor of node 5 and node 1 is node 3.</pre> |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +<p><strong>Example 2:</strong></p> |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +<pre> |
| 51 | + |
| 52 | +<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 |
| 53 | + |
| 54 | +<strong>Output:</strong> 5 |
| 55 | + |
| 56 | +<strong>Explanation:</strong> The first common ancestor of node 5 and node 4 is node 5.</pre> |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +<p><strong>Notes:</strong></p> |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +<ul> |
| 65 | + |
| 66 | + <li>All node values are pairwise distinct.</li> |
| 67 | + |
| 68 | + <li>p, q are different node and both can be found in the given tree.</li> |
| 69 | + |
| 70 | +</ul> |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +## Solutions |
| 76 | + |
| 77 | + |
| 78 | +### Python3 |
| 79 | + |
| 80 | +```python |
| 81 | +# Definition for a binary tree node. |
| 82 | +# class TreeNode: |
| 83 | +# def __init__(self, x): |
| 84 | +# self.val = x |
| 85 | +# self.left = None |
| 86 | +# self.right = None |
| 87 | + |
| 88 | +class Solution: |
| 89 | + def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode: |
| 90 | + if root is None or root == p or root == q: |
| 91 | + return root |
| 92 | + left = self.lowestCommonAncestor(root.left, p, q) |
| 93 | + right = self.lowestCommonAncestor(root.right, p, q) |
| 94 | + return right if left is None else (left if right is None else root) |
| 95 | +``` |
| 96 | + |
| 97 | +### Java |
| 98 | + |
| 99 | +```java |
| 100 | +/** |
| 101 | + * Definition for a binary tree node. |
| 102 | + * public class TreeNode { |
| 103 | + * int val; |
| 104 | + * TreeNode left; |
| 105 | + * TreeNode right; |
| 106 | + * TreeNode(int x) { val = x; } |
| 107 | + * } |
| 108 | + */ |
| 109 | +class Solution { |
| 110 | + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { |
| 111 | + if (root == null || root == p || root == q) { |
| 112 | + return root; |
| 113 | + } |
| 114 | + TreeNode left = lowestCommonAncestor(root.left, p, q); |
| 115 | + TreeNode right = lowestCommonAncestor(root.right, p, q); |
| 116 | + return left == null ? right : (right == null ? left : root); |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### ... |
| 122 | +``` |
| 123 | +
|
| 124 | +``` |
0 commit comments