Skip to content

Commit 6a9757a

Browse files
committed
Merge pull request #211 from 0xff-dev/master
Add solution and test-cases for problem 653
2 parents fa1a01f + b7e570c commit 6a9757a

File tree

5 files changed

+100
-21
lines changed

5 files changed

+100
-21
lines changed

leetcode/601-700/0653.Two-Sum-IV--Input-is-a-BST/README.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
# [653.Two Sum IV - Input is a BST][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given the `root` of a Binary Search Tree and a target number `k`, return `true` if there exist two elements in the BST such that their sum is equal to the given target.
5+
76

87
**Example 1:**
8+
![example1](./sum_tree_1.jpg)
9+
```
10+
Input: root = [5,3,6,2,4,null,7], k = 9
11+
Output: true
12+
```
913

14+
__Example 2:__
15+
![example2](./sum_tree_2.jpg)
1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: root = [5,3,6,2,4,null,7], k = 28
18+
Output: false
1319
```
1420

15-
## 题意
16-
> ...
21+
__Example 3:__
1722

18-
## 题解
23+
```
24+
Input: root = [2,1,3], k = 4
25+
Output: true
26+
```
1927

20-
### 思路1
21-
> ...
22-
Two Sum IV - Input is a BST
23-
```go
28+
__Example 4:__
29+
30+
```
31+
Input: root = [2,1,3], k = 1
32+
Output: false
2433
```
2534

35+
__Example 5:__
36+
37+
```
38+
Input: root = [2,1,3], k = 3
39+
Output: true
40+
```
2641

2742
## 结语
2843

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func Solution(root *TreeNode, k int) bool {
10+
nodes := make([]int, 0)
11+
treeArray(root, &nodes)
12+
return binarySearch(nodes, k)
13+
}
14+
15+
func treeArray(root *TreeNode, nodes *[]int) {
16+
if root == nil {
17+
return
18+
}
19+
if root.Left != nil {
20+
treeArray(root.Left, nodes)
21+
}
22+
23+
*nodes = append(*nodes, root.Val)
24+
25+
if root.Right != nil {
26+
treeArray(root.Right, nodes)
27+
}
28+
}
29+
func binarySearch(nodes []int, target int) bool {
30+
start, end := 0, len(nodes)-1
31+
for start < end {
32+
num := nodes[start] + nodes[end]
33+
if num == target {
34+
return true
35+
}
36+
if num < target {
37+
start++
38+
continue
39+
}
40+
end--
41+
}
42+
43+
return false
544
}

leetcode/601-700/0653.Two-Sum-IV--Input-is-a-BST/Solution_test.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,46 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs *TreeNode
14+
target int
1415
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", &TreeNode{
18+
Val: 5,
19+
Left: &TreeNode{
20+
Val: 3,
21+
Left: &TreeNode{Val: 2},
22+
Right: &TreeNode{Val: 4},
23+
},
24+
Right: &TreeNode{
25+
Val: 6,
26+
Right: &TreeNode{Val: 7},
27+
},
28+
}, 9, true},
29+
{"TestCase2", &TreeNode{
30+
Val: 5,
31+
Left: &TreeNode{
32+
Val: 3,
33+
Left: &TreeNode{Val: 2},
34+
Right: &TreeNode{Val: 4},
35+
},
36+
Right: &TreeNode{
37+
Val: 6,
38+
Right: &TreeNode{Val: 7},
39+
},
40+
}, 28, false},
41+
{"TestCase3", &TreeNode{Val: 2, Left: &TreeNode{Val: 1}, Right: &TreeNode{Val: 3}}, 4, true},
42+
{"TestCase4", &TreeNode{Val: 2, Left: &TreeNode{Val: 1}, Right: &TreeNode{Val: 3}}, 1, false},
43+
{"TestCase4", &TreeNode{Val: 2, Left: &TreeNode{Val: 1}, Right: &TreeNode{Val: 3}}, 3, true},
1944
}
2045

2146
// 开始测试
2247
for i, c := range cases {
2348
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
49+
got := Solution(c.inputs, c.target)
2550
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
51+
t.Fatalf("expected: %v, but got: %v, with inputs: %v, target: %v",
52+
c.expect, got, c.inputs, c.target)
2853
}
2954
})
3055
}
Loading
Loading

0 commit comments

Comments
 (0)