Skip to content

Commit 0b64aa5

Browse files
committed
feat: add solutions to lc problem: No.1305
No.1395.All Elements in Two Binary Search Trees
1 parent 8cd1926 commit 0b64aa5

File tree

6 files changed

+544
-1
lines changed

6 files changed

+544
-1
lines changed

solution/1300-1399/1305.All Elements in Two Binary Search Trees/README.md

+186-1
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,207 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
二叉树中序遍历 + 有序列表归并。
63+
6264
<!-- tabs:start -->
6365

6466
### **Python3**
6567

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

6870
```python
69-
71+
# Definition for a binary tree node.
72+
# class TreeNode:
73+
# def __init__(self, val=0, left=None, right=None):
74+
# self.val = val
75+
# self.left = left
76+
# self.right = right
77+
class Solution:
78+
def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]:
79+
def dfs(root, t):
80+
if root is None:
81+
return
82+
dfs(root.left, t)
83+
t.append(root.val)
84+
dfs(root.right, t)
85+
86+
def merge(t1, t2):
87+
ans = []
88+
i = j = 0
89+
while i < len(t1) and j < len(t2):
90+
if t1[i] <= t2[j]:
91+
ans.append(t1[i])
92+
i += 1
93+
else:
94+
ans.append(t2[j])
95+
j += 1
96+
while i < len(t1):
97+
ans.append(t1[i])
98+
i += 1
99+
while j < len(t2):
100+
ans.append(t2[j])
101+
j += 1
102+
return ans
103+
104+
t1, t2 = [], []
105+
dfs(root1, t1)
106+
dfs(root2, t2)
107+
return merge(t1, t2)
70108
```
71109

72110
### **Java**
73111

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

76114
```java
115+
/**
116+
* Definition for a binary tree node.
117+
* public class TreeNode {
118+
* int val;
119+
* TreeNode left;
120+
* TreeNode right;
121+
* TreeNode() {}
122+
* TreeNode(int val) { this.val = val; }
123+
* TreeNode(int val, TreeNode left, TreeNode right) {
124+
* this.val = val;
125+
* this.left = left;
126+
* this.right = right;
127+
* }
128+
* }
129+
*/
130+
class Solution {
131+
public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
132+
List<Integer> t1 = new ArrayList<>();
133+
List<Integer> t2 = new ArrayList<>();
134+
dfs(root1, t1);
135+
dfs(root2, t2);
136+
return merge(t1, t2);
137+
}
138+
139+
private void dfs(TreeNode root, List<Integer> t) {
140+
if (root == null) {
141+
return;
142+
}
143+
dfs(root.left, t);
144+
t.add(root.val);
145+
dfs(root.right, t);
146+
}
147+
148+
private List<Integer> merge(List<Integer> t1, List<Integer> t2) {
149+
List<Integer> ans = new ArrayList<>();
150+
int i = 0, j = 0;
151+
while (i < t1.size() && j < t2.size()) {
152+
if (t1.get(i) <= t2.get(j)) {
153+
ans.add(t1.get(i++));
154+
} else {
155+
ans.add(t2.get(j++));
156+
}
157+
}
158+
while (i < t1.size()) {
159+
ans.add(t1.get(i++));
160+
}
161+
while (j < t2.size()) {
162+
ans.add(t2.get(j++));
163+
}
164+
return ans;
165+
}
166+
}
167+
```
168+
169+
### **C++**
170+
171+
```cpp
172+
/**
173+
* Definition for a binary tree node.
174+
* struct TreeNode {
175+
* int val;
176+
* TreeNode *left;
177+
* TreeNode *right;
178+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
179+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
180+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
181+
* };
182+
*/
183+
class Solution {
184+
public:
185+
vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
186+
vector<int> t1;
187+
vector<int> t2;
188+
dfs(root1, t1);
189+
dfs(root2, t2);
190+
return merge(t1, t2);
191+
}
192+
193+
void dfs(TreeNode* root, vector<int>& t) {
194+
if (!root) return;
195+
dfs(root->left, t);
196+
t.push_back(root->val);
197+
dfs(root->right, t);
198+
}
199+
200+
vector<int> merge(vector<int>& t1, vector<int>& t2) {
201+
vector<int> ans;
202+
int i = 0, j = 0;
203+
while (i < t1.size() && j < t2.size())
204+
{
205+
if (t1[i] <= t2[j]) ans.push_back(t1[i++]);
206+
else ans.push_back(t2[j++]);
207+
}
208+
while (i < t1.size()) ans.push_back(t1[i++]);
209+
while (j < t2.size()) ans.push_back(t2[j++]);
210+
return ans;
211+
}
212+
};
213+
```
77214
215+
### **Go**
216+
217+
```go
218+
/**
219+
* Definition for a binary tree node.
220+
* type TreeNode struct {
221+
* Val int
222+
* Left *TreeNode
223+
* Right *TreeNode
224+
* }
225+
*/
226+
func getAllElements(root1 *TreeNode, root2 *TreeNode) []int {
227+
var dfs func(root *TreeNode) []int
228+
dfs = func(root *TreeNode) []int {
229+
if root == nil {
230+
return []int{}
231+
}
232+
left := dfs(root.Left)
233+
right := dfs(root.Right)
234+
left = append(left, root.Val)
235+
left = append(left, right...)
236+
return left
237+
}
238+
merge := func(t1, t2 []int) []int {
239+
var ans []int
240+
i, j := 0, 0
241+
for i < len(t1) && j < len(t2) {
242+
if t1[i] <= t2[j] {
243+
ans = append(ans, t1[i])
244+
i++
245+
} else {
246+
ans = append(ans, t2[j])
247+
j++
248+
}
249+
}
250+
for i < len(t1) {
251+
ans = append(ans, t1[i])
252+
i++
253+
}
254+
for j < len(t2) {
255+
ans = append(ans, t2[j])
256+
j++
257+
}
258+
return ans
259+
}
260+
t1, t2 := dfs(root1), dfs(root2)
261+
return merge(t1, t2)
262+
}
78263
```
79264

80265
### **...**

0 commit comments

Comments
 (0)