File tree 3 files changed +130
-25
lines changed 3 files changed +130
-25
lines changed Original file line number Diff line number Diff line change 64
64
| [ 561] [ 561-question ] | [ Array Partition I] [ 561-tips ] | [ ✅] [ 561-java ] | [ ✅] [ 561-js ] | |
65
65
| [ 643] [ 643-question ] | [ Maximum Average Subarray I] [ 643-tips ] | [ ✅] [ 643-java ] | [ ✅] [ 643-js ] | |
66
66
| [ 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 ] | |
68
68
| [ 657] [ 657-question ] | [ Judge Route Circle] [ 657-tips ] | | [ ✅] [ 657-js ] | |
69
69
| [ 661] [ 661-question ] | [ Image Smoother] [ 661-tips ] | | [ ✅] [ 661-js ] | |
70
70
| [ 665] [ 665-question ] | [ Non-decreasing Array] [ 665-tips ] | | [ ✅] [ 665-js ] | |
455
455
[ 561-java ] : ./src/_561/Solution.java
456
456
[ 643-java ] : ./src/_643/Solution.java
457
457
[ 645-java ] : ./src/_645/Solution.java
458
+ [ 653-java ] : ./src/_653/Solution.java
458
459
[ 728-java ] : ./src/_728/Solution.java
459
460
[ 771-java ] : ./src/_771/Solution.java
460
461
[ 804-java ] : ./src/_804/Solution.java
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- [ xxxx ] [ title ]
1
+ [ Two Sum IV - Input is a BST ] [ title ]
2
2
3
3
## 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.
5
5
6
+ ** Example 1:**
6
7
7
- ** Example:**
8
-
9
- ```
10
- // 抄Example
11
8
```
9
+ Input:
10
+ 5
11
+ / \
12
+ 3 6
13
+ / \ \
14
+ 2 4 7
12
15
13
- ** Note:**
14
- // Note
15
-
16
- ** Tags:** // tags
16
+ Target = 9
17
17
18
+ Output: True
19
+ ```
18
20
19
- ## 思路 1
20
- // 贴一些关键代码,说一些解题思路
21
- // (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22
- ``` java
21
+ ** Example 2:**
23
22
24
23
```
25
- ``` javascript
24
+ Input:
25
+ 5
26
+ / \
27
+ 3 6
28
+ / \ \
29
+ 2 4 7
30
+
31
+ Target = 28
26
32
33
+ Output: False
27
34
```
28
35
29
- ## 思路 2
30
- // 贴一些关键代码,说一些解题思路
31
- ``` java
36
+ ** Tags:** [ Tree] ( https://leetcode.com/tag/tree/ )
32
37
33
- ```
38
+ ## 思路
39
+
40
+ 题目要求在搜索二叉树中找到两个数,使其和等于所给值。此题主要考验搜索二叉树的用法。先遍历二叉树,在遍历过程中利用搜索二叉树的特性,查找是否存在另一个节点的值使两个节点的和等于所给的值。例如在遍历到节点a时,查找二叉树中是否存在节点b使a+b=k成立。
34
41
35
- ## 思路 3
36
- // 贴一些关键代码,说一些解题思路
37
- ``` kotlin
42
+ ** Java**
38
43
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
+ }
39
84
```
40
85
41
86
## 结语
42
-
87
+
43
88
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[ LeetCode-Solution] [ ls ]
44
89
45
- [ title ] : https://leetcode.com/problems/xxxx
90
+ [ title ] : https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/
46
91
[ ls ] : https://github.com/SDE603/LeetCode-Solution
You can’t perform that action at this time.
0 commit comments