Skip to content

Commit ca2f8d6

Browse files
committed
feat: add python and java solutions to lcci question
添加《程序员面试金典》题解:面试题 04.08. 首个共同祖先
1 parent 0a34570 commit ca2f8d6

File tree

4 files changed

+190
-61
lines changed

4 files changed

+190
-61
lines changed

lcci/04.08.First Common Ancestor/README.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,45 @@
1212
<!-- 这里可写当前语言的特殊实现逻辑 -->
1313

1414
```python
15-
15+
# Definition for a binary tree node.
16+
# class TreeNode:
17+
# def __init__(self, x):
18+
# self.val = x
19+
# self.left = None
20+
# self.right = None
21+
22+
class Solution:
23+
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
24+
if root is None or root == p or root == q:
25+
return root
26+
left = self.lowestCommonAncestor(root.left, p, q)
27+
right = self.lowestCommonAncestor(root.right, p, q)
28+
return right if left is None else (left if right is None else root)
1629
```
1730

1831
### Java
1932
<!-- 这里可写当前语言的特殊实现逻辑 -->
2033

2134
```java
22-
35+
/**
36+
* Definition for a binary tree node.
37+
* public class TreeNode {
38+
* int val;
39+
* TreeNode left;
40+
* TreeNode right;
41+
* TreeNode(int x) { val = x; }
42+
* }
43+
*/
44+
class Solution {
45+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
46+
if (root == null || root == p || root == q) {
47+
return root;
48+
}
49+
TreeNode left = lowestCommonAncestor(root.left, p, q);
50+
TreeNode right = lowestCommonAncestor(root.right, p, q);
51+
return left == null ? right : (right == null ? left : root);
52+
}
53+
}
2354
```
2455

2556
### ...
+124-59
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,124 @@
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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
12+
if (root == null || root == p || root == q) {
13+
return root;
14+
}
15+
TreeNode left = lowestCommonAncestor(root.left, p, q);
16+
TreeNode right = lowestCommonAncestor(root.right, p, q);
17+
return left == null ? right : (right == null ? left : root);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
10+
if root is None or root == p or root == q:
11+
return root
12+
left = self.lowestCommonAncestor(root.left, p, q)
13+
right = self.lowestCommonAncestor(root.right, p, q)
14+
return right if left is None else (left if right is None else root)

0 commit comments

Comments
 (0)