Skip to content

Commit 4d4ba6a

Browse files
committed
feat: add solutions to lc problem: No.1676
No.1676.Lowest Common Ancestor of a Binary Tree IV
1 parent 0c64f10 commit 4d4ba6a

File tree

5 files changed

+256
-2
lines changed

5 files changed

+256
-2
lines changed

solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README.md

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,109 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:哈希表 + DFS**
61+
6062
<!-- tabs:start -->
6163

6264
### **Python3**
6365

6466
<!-- 这里可写当前语言的特殊实现逻辑 -->
6567

6668
```python
67-
69+
# Definition for a binary tree node.
70+
# class TreeNode:
71+
# def __init__(self, x):
72+
# self.val = x
73+
# self.left = None
74+
# self.right = None
75+
76+
class Solution:
77+
def lowestCommonAncestor(self, root: 'TreeNode', nodes: 'List[TreeNode]') -> 'TreeNode':
78+
def dfs(root):
79+
if root is None or root.val in s:
80+
return root
81+
left, right = dfs(root.left), dfs(root.right)
82+
if left and right:
83+
return root
84+
return left or right
85+
86+
s = {node.val for node in nodes}
87+
return dfs(root)
6888
```
6989

7090
### **Java**
7191

7292
<!-- 这里可写当前语言的特殊实现逻辑 -->
7393

7494
```java
95+
/**
96+
* Definition for a binary tree node.
97+
* public class TreeNode {
98+
* int val;
99+
* TreeNode left;
100+
* TreeNode right;
101+
* TreeNode(int x) { val = x; }
102+
* }
103+
*/
104+
class Solution {
105+
private Set<Integer> s = new HashSet<>();
106+
107+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) {
108+
for (TreeNode node : nodes) {
109+
s.add(node.val);
110+
}
111+
return dfs(root);
112+
}
113+
114+
private TreeNode dfs(TreeNode root) {
115+
if (root == null || s.contains(root.val)) {
116+
return root;
117+
}
118+
TreeNode left = dfs(root.left);
119+
TreeNode right = dfs(root.right);
120+
if (left == null) {
121+
return right;
122+
}
123+
if (right == null) {
124+
return left;
125+
}
126+
return root;
127+
}
128+
}
129+
```
75130

131+
### **C++**
132+
133+
```cpp
134+
/**
135+
* Definition for a binary tree node.
136+
* struct TreeNode {
137+
* int val;
138+
* TreeNode *left;
139+
* TreeNode *right;
140+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
141+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
142+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
143+
* };
144+
*/
145+
class Solution {
146+
public:
147+
unordered_set<int> s;
148+
149+
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*> &nodes) {
150+
for (auto node : nodes) s.insert(node->val);
151+
return dfs(root);
152+
}
153+
154+
TreeNode* dfs(TreeNode* root) {
155+
if (!root || s.count(root->val)) return root;
156+
auto left = dfs(root->left);
157+
auto right = dfs(root->right);
158+
if (!left) return right;
159+
if (!right) return left;
160+
return root;
161+
}
162+
};
76163
```
77164
78165
### **...**

solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,98 @@
5252
### **Python3**
5353

5454
```python
55-
55+
# Definition for a binary tree node.
56+
# class TreeNode:
57+
# def __init__(self, x):
58+
# self.val = x
59+
# self.left = None
60+
# self.right = None
61+
62+
class Solution:
63+
def lowestCommonAncestor(self, root: 'TreeNode', nodes: 'List[TreeNode]') -> 'TreeNode':
64+
def dfs(root):
65+
if root is None or root.val in s:
66+
return root
67+
left, right = dfs(root.left), dfs(root.right)
68+
if left and right:
69+
return root
70+
return left or right
71+
72+
s = {node.val for node in nodes}
73+
return dfs(root)
5674
```
5775

5876
### **Java**
5977

6078
```java
79+
/**
80+
* Definition for a binary tree node.
81+
* public class TreeNode {
82+
* int val;
83+
* TreeNode left;
84+
* TreeNode right;
85+
* TreeNode(int x) { val = x; }
86+
* }
87+
*/
88+
class Solution {
89+
private Set<Integer> s = new HashSet<>();
90+
91+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) {
92+
for (TreeNode node : nodes) {
93+
s.add(node.val);
94+
}
95+
return dfs(root);
96+
}
97+
98+
private TreeNode dfs(TreeNode root) {
99+
if (root == null || s.contains(root.val)) {
100+
return root;
101+
}
102+
TreeNode left = dfs(root.left);
103+
TreeNode right = dfs(root.right);
104+
if (left == null) {
105+
return right;
106+
}
107+
if (right == null) {
108+
return left;
109+
}
110+
return root;
111+
}
112+
}
113+
```
61114

115+
### **C++**
116+
117+
```cpp
118+
/**
119+
* Definition for a binary tree node.
120+
* struct TreeNode {
121+
* int val;
122+
* TreeNode *left;
123+
* TreeNode *right;
124+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
125+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
126+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
127+
* };
128+
*/
129+
class Solution {
130+
public:
131+
unordered_set<int> s;
132+
133+
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*> &nodes) {
134+
for (auto node : nodes) s.insert(node->val);
135+
return dfs(root);
136+
}
137+
138+
TreeNode* dfs(TreeNode* root) {
139+
if (!root || s.count(root->val)) return root;
140+
auto left = dfs(root->left);
141+
auto right = dfs(root->right);
142+
if (!left) return right;
143+
if (!right) return left;
144+
return root;
145+
}
146+
};
62147
```
63148
64149
### **...**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
unordered_set<int> s;
15+
16+
TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*> &nodes) {
17+
for (auto node : nodes) s.insert(node->val);
18+
return dfs(root);
19+
}
20+
21+
TreeNode* dfs(TreeNode* root) {
22+
if (!root || s.count(root->val)) return root;
23+
auto left = dfs(root->left);
24+
auto right = dfs(root->right);
25+
if (!left) return right;
26+
if (!right) return left;
27+
return root;
28+
}
29+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
private Set<Integer> s = new HashSet<>();
12+
13+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) {
14+
for (TreeNode node : nodes) {
15+
s.add(node.val);
16+
}
17+
return dfs(root);
18+
}
19+
20+
private TreeNode dfs(TreeNode root) {
21+
if (root == null || s.contains(root.val)) {
22+
return root;
23+
}
24+
TreeNode left = dfs(root.left);
25+
TreeNode right = dfs(root.right);
26+
if (left == null) {
27+
return right;
28+
}
29+
if (right == null) {
30+
return left;
31+
}
32+
return root;
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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', nodes: 'List[TreeNode]') -> 'TreeNode':
10+
def dfs(root):
11+
if root is None or root.val in s:
12+
return root
13+
left, right = dfs(root.left), dfs(root.right)
14+
if left and right:
15+
return root
16+
return left or right
17+
18+
s = {node.val for node in nodes}
19+
return dfs(root)

0 commit comments

Comments
 (0)