Skip to content

Commit 0975816

Browse files
committed
feat: add the solution of Two Sum IV - Input is a BST(653) with Java.
1 parent 0f0a54a commit 0975816

File tree

3 files changed

+130
-25
lines changed

3 files changed

+130
-25
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
| [561][561-question] | [Array Partition I][561-tips] | [][561-java] | [][561-js] | |
6565
| [643][643-question] | [Maximum Average Subarray I][643-tips] | [][643-java] | [][643-js] | |
6666
| [645][645-question] | [Set Mismatch][645-tips] | [][645-java] | [][645-js] | |
67-
| [653][653-question] | [Two Sum IV - Input is a BST][653-tips] | | [][653-js] | |
67+
| [653][653-question] | [Two Sum IV - Input is a BST][653-tips] | [][653-java] | [][653-js] | |
6868
| [657][657-question] | [Judge Route Circle][657-tips] | | [][657-js] | |
6969
| [661][661-question] | [Image Smoother][661-tips] | | [][661-js] | |
7070
| [665][665-question] | [Non-decreasing Array][665-tips] | | [][665-js] | |
@@ -455,6 +455,7 @@
455455
[561-java]: ./src/_561/Solution.java
456456
[643-java]: ./src/_643/Solution.java
457457
[645-java]: ./src/_645/Solution.java
458+
[653-java]: ./src/_653/Solution.java
458459
[728-java]: ./src/_728/Solution.java
459460
[771-java]: ./src/_771/Solution.java
460461
[804-java]: ./src/_804/Solution.java

src/_653/Solution.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package _653;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* public class TreeNode {
8+
* int val;
9+
* TreeNode left;
10+
* TreeNode right;
11+
* TreeNode(int x) { val = x; }
12+
* }
13+
*/
14+
class Solution {
15+
public boolean findTarget(TreeNode root, int k) {
16+
Stack<TreeNode> stack = new Stack<>();
17+
int num;
18+
TreeNode next;
19+
stack.push(root);
20+
while (!stack.empty()) {
21+
TreeNode node = stack.pop();
22+
23+
next = root;
24+
num = k - node.val;
25+
while (next != null) {
26+
if (next.val == num) {
27+
if (next == node) {
28+
break;
29+
} else {
30+
return true;
31+
}
32+
} else if (num > next.val) {
33+
next = next.right;
34+
} else {
35+
next = next.left;
36+
}
37+
}
38+
39+
if (node.left != null) {
40+
stack.push(node.left);
41+
}
42+
if (node.right != null) {
43+
stack.push(node.right);
44+
}
45+
}
46+
47+
return false;
48+
}
49+
50+
class TreeNode {
51+
int val;
52+
TreeNode left;
53+
TreeNode right;
54+
55+
TreeNode(int x) {
56+
val = x;
57+
}
58+
}
59+
}

tips/653/README.md

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,91 @@
1-
[xxxx][title]
1+
[Two Sum IV - Input is a BST][title]
22

33
## Description
4-
// 抄题目
4+
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.
55

6+
**Example 1:**
67

7-
**Example:**
8-
9-
```
10-
// 抄Example
118
```
9+
Input:
10+
5
11+
/ \
12+
3 6
13+
/ \ \
14+
2 4 7
1215
13-
**Note:**
14-
// Note
15-
16-
**Tags:** // tags
16+
Target = 9
1717
18+
Output: True
19+
```
1820

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
21+
**Example 2:**
2322

2423
```
25-
```javascript
24+
Input:
25+
5
26+
/ \
27+
3 6
28+
/ \ \
29+
2 4 7
30+
31+
Target = 28
2632
33+
Output: False
2734
```
2835

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
36+
**Tags:** [Tree](https://leetcode.com/tag/tree/)
3237

33-
```
38+
## 思路
39+
40+
题目要求在搜索二叉树中找到两个数,使其和等于所给值。此题主要考验搜索二叉树的用法。先遍历二叉树,在遍历过程中利用搜索二叉树的特性,查找是否存在另一个节点的值使两个节点的和等于所给的值。例如在遍历到节点a时,查找二叉树中是否存在节点b使a+b=k成立。
3441

35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
42+
**Java**
3843

44+
```java
45+
class Solution {
46+
public boolean findTarget(TreeNode root, int k) {
47+
Stack<TreeNode> stack = new Stack<>();
48+
int num;
49+
TreeNode next;
50+
51+
// 遍历二叉树
52+
stack.push(root);
53+
while (!stack.empty()) {
54+
TreeNode node = stack.pop();
55+
56+
// 查找二叉树
57+
next = root;
58+
num = k - node.val;
59+
while (next != null) {
60+
if (next.val == num) {
61+
if (next == node) {
62+
break;
63+
} else {
64+
return true;
65+
}
66+
} else if (num > next.val) {
67+
next = next.right;
68+
} else {
69+
next = next.left;
70+
}
71+
}
72+
73+
if (node.left != null) {
74+
stack.push(node.left);
75+
}
76+
if (node.right != null) {
77+
stack.push(node.right);
78+
}
79+
}
80+
81+
return false;
82+
}
83+
}
3984
```
4085

4186
## 结语
42-
87+
4388
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4489

45-
[title]: https://leetcode.com/problems/xxxx
90+
[title]: https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/
4691
[ls]: https://github.com/SDE603/LeetCode-Solution

0 commit comments

Comments
 (0)