Skip to content

Commit 2d9662a

Browse files
committed
feat: add solutions to lc problem: No.1214.Two Sum BSTs
1 parent 381435a commit 2d9662a

File tree

6 files changed

+462
-6
lines changed

6 files changed

+462
-6
lines changed

solution/1200-1299/1214.Two Sum BSTs/README.md

+159-2
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,184 @@
3737
<li><code>-10^9 &lt;= target, node.val &lt;= 10^9</code></li>
3838
</ol>
3939

40-
4140
## 解法
4241

4342
<!-- 这里可写通用的实现逻辑 -->
4443

44+
先中序遍历二叉搜索树 root1、root2 得到两个有序列表 vals1、vals2。然后利用双指针,i 指向 vals1 头部,j 指向 vals2 尾部,判断双指针指向的两元素和 s 与 target 的大小关系,若相等,直接返回 true,若 `s < target`,i 指针往右移动,否则 j 指针往左移动。
45+
46+
遍历结束,说明不存在两节点和为 target 的元素,直接返回 false。
47+
4548
<!-- tabs:start -->
4649

4750
### **Python3**
4851

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

5154
```python
52-
55+
# Definition for a binary tree node.
56+
# class TreeNode:
57+
# def __init__(self, val=0, left=None, right=None):
58+
# self.val = val
59+
# self.left = left
60+
# self.right = right
61+
class Solution:
62+
def twoSumBSTs(self, root1: TreeNode, root2: TreeNode, target: int) -> bool:
63+
vals1, vals2 = [], []
64+
65+
def inorder(root, vals):
66+
if root:
67+
inorder(root.left, vals)
68+
vals.append(root.val)
69+
inorder(root.right, vals)
70+
71+
inorder(root1, vals1)
72+
inorder(root2, vals2)
73+
74+
i, j = 0, len(vals2) - 1
75+
while i < len(vals1) and j >= 0:
76+
if vals1[i] + vals2[j] == target:
77+
return True
78+
if vals1[i] + vals2[j] < target:
79+
i += 1
80+
else:
81+
j -= 1
82+
return False
5383
```
5484

5585
### **Java**
5686

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

5989
```java
90+
/**
91+
* Definition for a binary tree node.
92+
* public class TreeNode {
93+
* int val;
94+
* TreeNode left;
95+
* TreeNode right;
96+
* TreeNode() {}
97+
* TreeNode(int val) { this.val = val; }
98+
* TreeNode(int val, TreeNode left, TreeNode right) {
99+
* this.val = val;
100+
* this.left = left;
101+
* this.right = right;
102+
* }
103+
* }
104+
*/
105+
class Solution {
106+
public boolean twoSumBSTs(TreeNode root1, TreeNode root2, int target) {
107+
List<Integer> vals1 = new ArrayList<>();
108+
List<Integer> vals2 = new ArrayList<>();
109+
inorder(root1, vals1);
110+
inorder(root2, vals2);
111+
int i = 0, j = vals2.size() - 1;
112+
while (i < vals1.size() && j >= 0) {
113+
int s = vals1.get(i) + vals2.get(j);
114+
if (s == target) {
115+
return true;
116+
}
117+
if (s < target) {
118+
++i;
119+
} else {
120+
--j;
121+
}
122+
}
123+
return false;
124+
}
125+
126+
private void inorder(TreeNode root, List<Integer> vals) {
127+
if (root != null) {
128+
inorder(root.left, vals);
129+
vals.add(root.val);
130+
inorder(root.right, vals);
131+
}
132+
}
133+
}
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
/**
140+
* Definition for a binary tree node.
141+
* struct TreeNode {
142+
* int val;
143+
* TreeNode *left;
144+
* TreeNode *right;
145+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
146+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
147+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
148+
* };
149+
*/
150+
class Solution {
151+
public:
152+
bool twoSumBSTs(TreeNode *root1, TreeNode *root2, int target) {
153+
vector<int> vals1, vals2;
154+
inorder(root1, vals1);
155+
inorder(root2, vals2);
156+
int i = 0, j = vals2.size() - 1;
157+
while (i < vals1.size() && j >= 0)
158+
{
159+
int s = vals1[i] + vals2[j];
160+
if (s == target)
161+
return true;
162+
if (s < target)
163+
++i;
164+
else
165+
--j;
166+
}
167+
return false;
168+
}
169+
170+
void inorder(TreeNode *root, vector<int> &vals) {
171+
if (root)
172+
{
173+
inorder(root->left, vals);
174+
vals.push_back(root->val);
175+
inorder(root->right, vals);
176+
}
177+
}
178+
};
179+
```
60180

181+
### **Go**
182+
183+
```go
184+
/**
185+
* Definition for a binary tree node.
186+
* type TreeNode struct {
187+
* Val int
188+
* Left *TreeNode
189+
* Right *TreeNode
190+
* }
191+
*/
192+
func twoSumBSTs(root1 *TreeNode, root2 *TreeNode, target int) bool {
193+
vals1 := inorder(root1)
194+
vals2 := inorder(root2)
195+
i, j := 0, len(vals2)-1
196+
for i < len(vals1) && j >= 0 {
197+
s := vals1[i] + vals2[j]
198+
if s == target {
199+
return true
200+
}
201+
if s < target {
202+
i++
203+
} else {
204+
j--
205+
}
206+
}
207+
return false
208+
}
209+
210+
func inorder(root *TreeNode) []int {
211+
if root == nil {
212+
return nil
213+
}
214+
left := inorder(root.Left)
215+
right := inorder(root.Right)
216+
return append(append(left, root.Val), right...)
217+
}
61218
```
62219

63220
### **...**

solution/1200-1299/1214.Two Sum BSTs/README_EN.md

+157-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
<p>&nbsp;</p>
1010
<p><strong>Example 1:</strong></p>
11-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1214.Two%20Sum%20BSTs/images/ex1.png" style="width: 369px; height: 169px;" />
11+
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1214.Two%20Sum%20BSTs/images/1368_1_a2.png" style="height: 140px; width: 150px;"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1214.Two%20Sum%20BSTs/images/1368_1_b.png" style="height: 136px; width: 150px;"></strong></p>
1212
<pre>
1313
<strong>Input:</strong> root1 = [2,1,4], root2 = [1,0,3], target = 5
1414
<strong>Output:</strong> true
1515
<strong>Explanation: </strong>2 and 3 sum up to 5.
1616
</pre>
1717

1818
<p><strong>Example 2:</strong></p>
19-
<img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1214.Two%20Sum%20BSTs/images/ex2.png" style="width: 453px; height: 290px;" />
19+
<p><strong><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1214.Two%20Sum%20BSTs/images/1368_2_a.png" style="height: 137px; width: 150px;"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/1200-1299/1214.Two%20Sum%20BSTs/images/1368_2_b.png" style="height: 168px; width: 150px;"></strong></p>
2020
<pre>
2121
<strong>Input:</strong> root1 = [0,-10,10], root2 = [5,1,7,0,2], target = 18
2222
<strong>Output:</strong> false
@@ -30,21 +30,174 @@
3030
<li><code>-10<sup>9</sup> &lt;= Node.val, target &lt;= 10<sup>9</sup></code></li>
3131
</ul>
3232

33-
3433
## Solutions
3534

3635
<!-- tabs:start -->
3736

3837
### **Python3**
3938

4039
```python
41-
40+
# Definition for a binary tree node.
41+
# class TreeNode:
42+
# def __init__(self, val=0, left=None, right=None):
43+
# self.val = val
44+
# self.left = left
45+
# self.right = right
46+
class Solution:
47+
def twoSumBSTs(self, root1: TreeNode, root2: TreeNode, target: int) -> bool:
48+
vals1, vals2 = [], []
49+
50+
def inorder(root, vals):
51+
if root:
52+
inorder(root.left, vals)
53+
vals.append(root.val)
54+
inorder(root.right, vals)
55+
56+
inorder(root1, vals1)
57+
inorder(root2, vals2)
58+
59+
i, j = 0, len(vals2) - 1
60+
while i < len(vals1) and j >= 0:
61+
if vals1[i] + vals2[j] == target:
62+
return True
63+
if vals1[i] + vals2[j] < target:
64+
i += 1
65+
else:
66+
j -= 1
67+
return False
4268
```
4369

4470
### **Java**
4571

4672
```java
73+
/**
74+
* Definition for a binary tree node.
75+
* public class TreeNode {
76+
* int val;
77+
* TreeNode left;
78+
* TreeNode right;
79+
* TreeNode() {}
80+
* TreeNode(int val) { this.val = val; }
81+
* TreeNode(int val, TreeNode left, TreeNode right) {
82+
* this.val = val;
83+
* this.left = left;
84+
* this.right = right;
85+
* }
86+
* }
87+
*/
88+
class Solution {
89+
public boolean twoSumBSTs(TreeNode root1, TreeNode root2, int target) {
90+
List<Integer> vals1 = new ArrayList<>();
91+
List<Integer> vals2 = new ArrayList<>();
92+
inorder(root1, vals1);
93+
inorder(root2, vals2);
94+
int i = 0, j = vals2.size() - 1;
95+
while (i < vals1.size() && j >= 0) {
96+
int s = vals1.get(i) + vals2.get(j);
97+
if (s == target) {
98+
return true;
99+
}
100+
if (s < target) {
101+
++i;
102+
} else {
103+
--j;
104+
}
105+
}
106+
return false;
107+
}
108+
109+
private void inorder(TreeNode root, List<Integer> vals) {
110+
if (root != null) {
111+
inorder(root.left, vals);
112+
vals.add(root.val);
113+
inorder(root.right, vals);
114+
}
115+
}
116+
}
117+
```
118+
119+
### **C++**
120+
121+
```cpp
122+
/**
123+
* Definition for a binary tree node.
124+
* struct TreeNode {
125+
* int val;
126+
* TreeNode *left;
127+
* TreeNode *right;
128+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
129+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
130+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
131+
* };
132+
*/
133+
class Solution {
134+
public:
135+
bool twoSumBSTs(TreeNode *root1, TreeNode *root2, int target) {
136+
vector<int> vals1, vals2;
137+
inorder(root1, vals1);
138+
inorder(root2, vals2);
139+
int i = 0, j = vals2.size() - 1;
140+
while (i < vals1.size() && j >= 0)
141+
{
142+
int s = vals1[i] + vals2[j];
143+
if (s == target)
144+
return true;
145+
if (s < target)
146+
++i;
147+
else
148+
--j;
149+
}
150+
return false;
151+
}
152+
153+
void inorder(TreeNode *root, vector<int> &vals) {
154+
if (root)
155+
{
156+
inorder(root->left, vals);
157+
vals.push_back(root->val);
158+
inorder(root->right, vals);
159+
}
160+
}
161+
};
162+
```
47163

164+
### **Go**
165+
166+
```go
167+
/**
168+
* Definition for a binary tree node.
169+
* type TreeNode struct {
170+
* Val int
171+
* Left *TreeNode
172+
* Right *TreeNode
173+
* }
174+
*/
175+
func twoSumBSTs(root1 *TreeNode, root2 *TreeNode, target int) bool {
176+
vals1 := inorder(root1)
177+
vals2 := inorder(root2)
178+
i, j := 0, len(vals2)-1
179+
for i < len(vals1) && j >= 0 {
180+
s := vals1[i] + vals2[j]
181+
if s == target {
182+
return true
183+
}
184+
if s < target {
185+
i++
186+
} else {
187+
j--
188+
}
189+
}
190+
return false
191+
}
192+
193+
func inorder(root *TreeNode) []int {
194+
if root == nil {
195+
return nil
196+
}
197+
left := inorder(root.Left)
198+
right := inorder(root.Right)
199+
return append(append(left, root.Val), right...)
200+
}
48201
```
49202

50203
### **...**

0 commit comments

Comments
 (0)