Skip to content

Commit 5e64a4f

Browse files
committed
feat: 144 230
1 parent a9acc15 commit 5e64a4f

File tree

6 files changed

+204
-0
lines changed

6 files changed

+204
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# [144. Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)
2+
3+
## 2019/08/11
4+
5+
### 题目 💗[medium]
6+
7+
Given a binary tree, return the preorder traversal of its nodes' values.
8+
9+
Example:
10+
11+
```bash
12+
Input: [1,null,2,3]
13+
1
14+
\
15+
2
16+
/
17+
3
18+
19+
Output: [1,2,3]
20+
```
21+
22+
Follow up: Recursive solution is trivial, could you do it iteratively?
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package preorderTraversal
2+
3+
import (
4+
"github.com/pengliheng/leetcode/Helper"
5+
)
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* type TreeNode struct {
10+
* Val int
11+
* Left *TreeNode
12+
* Right *TreeNode
13+
* }
14+
*/
15+
16+
type TreeNode = Helper.TreeNode
17+
18+
// 1
19+
// 2 3
20+
// 4 5 6 7
21+
// 8
22+
23+
func preorderTraversal(root *TreeNode) []int {
24+
s := []*TreeNode{}
25+
res := []int{}
26+
for root != nil || len(s) > 0 {
27+
for root != nil {
28+
res = append(res, root.Val)
29+
s = append(s, root)
30+
root = root.Left
31+
}
32+
root = s[len(s)-1]
33+
s = s[:len(s)-1]
34+
root = root.Right
35+
}
36+
return res
37+
}
38+
39+
// func preorderTraversal(root *TreeNode) []int {
40+
// res:=[]int{}
41+
// var helper func(*TreeNode)
42+
// helper = func(root *TreeNode) {
43+
// if root == nil {
44+
// return
45+
// }
46+
// res = append(res, root.Val)
47+
// helper(root.Left)
48+
// helper(root.Right)
49+
// }
50+
// helper(root)
51+
// return res
52+
// }
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package preorderTraversal
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pengliheng/leetcode/Helper"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var tcs = []struct {
12+
N1 *TreeNode
13+
ans []int
14+
}{
15+
{
16+
Helper.Ints2TreeNode([]int{1, -1 << 63, 2, 3}),
17+
[]int{1, 2, 3},
18+
},
19+
}
20+
21+
func Test_bitwiseComplement(t *testing.T) {
22+
ast := assert.New(t)
23+
for _, tc := range tcs {
24+
ast.Equal(tc.ans, preorderTraversal(tc.N1), "输入:%v", tc)
25+
}
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [230. Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)
2+
3+
## 2019/08/11
4+
5+
### 题目 💗[medium]
6+
7+
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
8+
9+
Note:
10+
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
11+
12+
Example 1:
13+
14+
```bash
15+
Input: root = [3,1,4,null,2], k = 1
16+
3
17+
/ \
18+
1 4
19+
\
20+
2
21+
Output: 1
22+
```
23+
24+
Example 2:
25+
26+
```bash
27+
Input: root = [5,3,6,2,4,null,null,1], k = 3
28+
5
29+
/ \
30+
3 6
31+
/ \
32+
2 4
33+
/
34+
1
35+
Output: 3
36+
```
37+
38+
Follow up:
39+
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package kthSmallest
2+
3+
import (
4+
"github.com/pengliheng/leetcode/Helper"
5+
)
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* type TreeNode struct {
10+
* Val int
11+
* Left *TreeNode
12+
* Right *TreeNode
13+
* }
14+
*/
15+
type TreeNode = Helper.TreeNode
16+
17+
var min []int
18+
19+
func kthSmallest(root *TreeNode, k int) int {
20+
min = []int{}
21+
helper(root, k)
22+
return min[k-1]
23+
}
24+
25+
func helper(root *TreeNode, k int) {
26+
if root == nil {
27+
return
28+
}
29+
helper(root.Left, k-1)
30+
min = append(min, root.Val)
31+
helper(root.Right, k-1)
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package kthSmallest
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pengliheng/leetcode/Helper"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var tcs = []struct {
12+
N1 *TreeNode
13+
N2 int
14+
ans int
15+
}{
16+
{
17+
Helper.Ints2TreeNode([]int{3, 1, 4, -1 << 63, 2}),
18+
1,
19+
1,
20+
},
21+
{
22+
Helper.Ints2TreeNode([]int{5, 3, 6, 2, 4, -1 << 63, -1 << 63, 1}),
23+
3,
24+
3,
25+
},
26+
}
27+
28+
func Test_bitwiseComplement(t *testing.T) {
29+
ast := assert.New(t)
30+
for _, tc := range tcs {
31+
ast.Equal(tc.ans, kthSmallest(tc.N1, tc.N2), "输入:%v", tc)
32+
}
33+
}

0 commit comments

Comments
 (0)