Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41af90b

Browse files
author
陈世伟
committed
feat: 2021年05月12日18:59:59
1 parent 3496407 commit 41af90b

File tree

11 files changed

+457
-7
lines changed

11 files changed

+457
-7
lines changed

‎leetcode/0105/build_tree.go‎ renamed to ‎leetcode/105.从前序与中序遍历序列构造二叉树/build_tree.go‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package _105
1+
package _05_从前序与中序遍历序列构造二叉树
22

33
/**
44
* Definition for a binary tree node.
@@ -23,25 +23,33 @@ func buildTree(preorder []int, inorder []int) *TreeNode {
2323
return nil
2424
}
2525

26+
rootVal := preorder[0]
2627
rootNode := &TreeNode{
27-
Val: preorder[0],
28+
Val: rootVal,
2829
Left: nil,
2930
Right: nil,
3031
}
32+
preorder = preorder[1:]
3133

3234
// 找到根节点在中序遍历的位置
3335
// 根据中序遍历的特性 root节点左边的都是root节点的左子树,数量为(rootIndex+1)
3436
var rootIndex int
3537

3638
for i, v := range inorder {
37-
if v == preorder[0] {
39+
if v == rootVal {
3840
rootIndex = i
3941
break
4042
}
4143
}
4244

45+
leftInorder := inorder[:rootIndex]
46+
rightInorder := inorder[rootIndex+1:]
47+
48+
leftPreorder := preorder[:len(leftInorder)]
49+
rightPreorder := preorder[len(leftInorder):]
50+
4351
// 前序遍历的根节点的后面是左子树,左子树的切片为(1:rootIndex+1)
44-
rootNode.Left = buildTree(preorder[1:rootIndex+1], inorder[:rootIndex])
45-
rootNode.Right = buildTree(preorder[rootIndex+1:], inorder[rootIndex+1:])
52+
rootNode.Left = buildTree(leftPreorder, leftInorder)
53+
rootNode.Right = buildTree(rightPreorder, rightInorder)
4654
return rootNode
4755
}

‎leetcode/0105/build_tree_test.go‎ renamed to ‎leetcode/105.从前序与中序遍历序列构造二叉树/build_tree_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package _105
1+
package _05_从前序与中序遍历序列构造二叉树
22

33
import (
44
"reflect"
@@ -45,7 +45,7 @@ func Test_buildTree(t *testing.T) {
4545
}
4646
for _, tt := range tests {
4747
t.Run(tt.name, func(t *testing.T) {
48-
if got := buildTree(tt.args.preorder, tt.args.inorder); !reflect.DeepEqual(got, tt.want) {
48+
if got := buildTree1(tt.args.preorder, tt.args.inorder); !reflect.DeepEqual(got, tt.want) {
4949
t.Errorf("buildTree() = %v, want %v", got, tt.want)
5050
}
5151
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package _06_从中序与后序遍历序列构造二叉树
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
type TreeNode struct {
12+
Val int
13+
Left *TreeNode
14+
Right *TreeNode
15+
}
16+
17+
func buildTree(inorder []int, postorder []int) *TreeNode {
18+
if len(postorder) == 0 {
19+
return nil
20+
}
21+
// 从后序遍历获取root
22+
rootVal := postorder[len(postorder)-1]
23+
root := &TreeNode{
24+
Val: rootVal,
25+
}
26+
if len(postorder) == 1 {
27+
return root
28+
}
29+
// 从后序遍历删除root
30+
postorder = postorder[:len(postorder)-1]
31+
32+
// 获取root在中序遍历的位置
33+
var rootIdx int
34+
for i := 0; i < len(inorder); i++ {
35+
if inorder[i] == rootVal {
36+
rootIdx = i
37+
break
38+
}
39+
}
40+
41+
// root在中序遍历的位置切分左右子树,后续根据前序切分的数组长度切分后续数组
42+
root.Left = buildTree(inorder[:rootIdx], postorder[:len(inorder[:rootIdx])])
43+
root.Right = buildTree(inorder[rootIdx+1:], postorder[len(inorder[:rootIdx]):])
44+
return root
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package _06_从中序与后序遍历序列构造二叉树
2+
3+
import (
4+
"log"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func Test_buildTree(t *testing.T) {
10+
type args struct {
11+
inorder []int
12+
postorder []int
13+
}
14+
tests := []struct {
15+
name string
16+
args args
17+
want *TreeNode
18+
}{
19+
{name: `中序遍历 inorder = [9,3,15,20,7]
20+
后序遍历 postorder = [9,15,7,20,3]`, args: struct {
21+
inorder []int
22+
postorder []int
23+
}{inorder: []int{9, 3, 15, 20, 7}, postorder: []int{9, 15, 7, 20, 3}}},
24+
}
25+
for _, tt := range tests {
26+
t.Run(tt.name, func(t *testing.T) {
27+
got := buildTree(tt.args.inorder, tt.args.postorder)
28+
if !reflect.DeepEqual(got, tt.want) {
29+
t.Errorf("buildTree() = %v, want %v", got, tt.want)
30+
}
31+
32+
InEach(got)
33+
})
34+
}
35+
}
36+
37+
func InEach(root *TreeNode) {
38+
if root == nil {
39+
return
40+
}
41+
InEach(root.Left)
42+
log.Println(root.Val)
43+
InEach(root.Right)
44+
45+
return
46+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package _12_路径总和
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
12+
type TreeNode struct {
13+
Val int
14+
Left *TreeNode
15+
Right *TreeNode
16+
}
17+
18+
func hasPathSum(root *TreeNode, targetSum int) bool {
19+
if root == nil {
20+
return false
21+
}
22+
23+
if isLeafNode(root) && root.Val == targetSum {
24+
return true
25+
}
26+
27+
return hasPathSum(root.Left, targetSum-root.Val) || hasPathSum(root.Right, targetSum-root.Val)
28+
}
29+
30+
func isLeafNode(node *TreeNode) bool {
31+
return node != nil && node.Left == nil && node.Right == nil
32+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package _12_路径总和
2+
3+
import "testing"
4+
5+
func Test_hasPathSum(t *testing.T) {
6+
type args struct {
7+
root *TreeNode
8+
targetSum int
9+
}
10+
tests := []struct {
11+
name string
12+
args args
13+
want bool
14+
}{
15+
{name: `输入:root = [1,2,3], targetSum = 5
16+
输出:false`, args: args{
17+
root: &TreeNode{
18+
Val: 1,
19+
Left: &TreeNode{
20+
Val: 2,
21+
Left: nil,
22+
Right: nil,
23+
},
24+
Right: &TreeNode{
25+
Val: 3,
26+
Left: nil,
27+
Right: nil,
28+
},
29+
},
30+
targetSum: 5,
31+
}},
32+
}
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
if got := hasPathSum(tt.args.root, tt.args.targetSum); got != tt.want {
36+
t.Errorf("hasPathSum() = %v, want %v", got, tt.want)
37+
}
38+
})
39+
}
40+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package _13_路径总和II
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
type TreeNode struct {
12+
Val int
13+
Left *TreeNode
14+
Right *TreeNode
15+
}
16+
17+
func pathSum(root *TreeNode, targetSum int) [][]int {
18+
if root == nil {
19+
return nil
20+
}
21+
22+
var result [][]int
23+
24+
var each func(node *TreeNode, targetSum int, nums []int)
25+
26+
each = func(node *TreeNode, targetSum int, nums []int) {
27+
if node == nil {
28+
return
29+
}
30+
31+
newSums := make([]int, len(nums), cap(nums))
32+
copy(newSums, nums)
33+
34+
if IsLeafNode(node) && targetSum == node.Val {
35+
result = append(result, append(newSums, node.Val))
36+
return
37+
}
38+
39+
each(node.Left, targetSum-node.Val, append(newSums, node.Val))
40+
each(node.Right, targetSum-node.Val, append(newSums, node.Val))
41+
}
42+
each(root, targetSum, []int{})
43+
44+
return result
45+
}
46+
47+
func IsLeafNode(node *TreeNode) bool {
48+
if node == nil {
49+
return false
50+
}
51+
return node.Left == nil && node.Right == nil
52+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package _13_路径总和II
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func Test_pathSum(t *testing.T) {
9+
type args struct {
10+
root *TreeNode
11+
targetSum int
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want [][]int
17+
}{
18+
{name: `输入:root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
19+
输出:[[5,4,11,2],[5,8,4,5]]`, args: args{
20+
root: &TreeNode{
21+
Val: 5,
22+
Left: &TreeNode{
23+
Val: 4,
24+
Left: &TreeNode{
25+
Val: 11,
26+
Left: &TreeNode{
27+
Val: 7,
28+
Left: nil,
29+
Right: nil,
30+
},
31+
Right: &TreeNode{
32+
Val: 2,
33+
Left: nil,
34+
Right: nil,
35+
},
36+
},
37+
Right: nil,
38+
},
39+
Right: &TreeNode{
40+
Val: 8,
41+
Left: &TreeNode{
42+
Val: 13,
43+
Left: nil,
44+
Right: nil,
45+
},
46+
Right: &TreeNode{
47+
Val: 4,
48+
Left: &TreeNode{
49+
Val: 5,
50+
Left: nil,
51+
Right: nil,
52+
},
53+
Right: &TreeNode{
54+
Val: 1,
55+
Left: nil,
56+
Right: nil,
57+
},
58+
},
59+
},
60+
},
61+
targetSum: 22,
62+
}, want: [][]int{{5, 4, 11, 2}, {5, 8, 4, 5}}},
63+
{name: `输入:root = [1,2,3], targetSum = 5
64+
输出:[]`, args: args{
65+
root: &TreeNode{
66+
Val: 1,
67+
Left: &TreeNode{
68+
Val: 2,
69+
Left: nil,
70+
Right: nil,
71+
},
72+
Right: &TreeNode{
73+
Val: 3,
74+
Left: nil,
75+
Right: nil,
76+
},
77+
},
78+
targetSum: 5,
79+
}},
80+
}
81+
for _, tt := range tests {
82+
t.Run(tt.name, func(t *testing.T) {
83+
if got := pathSum(tt.args.root, tt.args.targetSum); !reflect.DeepEqual(got, tt.want) {
84+
t.Errorf("pathSum() = %v, want %v", got, tt.want)
85+
}
86+
})
87+
}
88+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package _04_左叶子之和
2+
3+
/**
4+
* Definition for a binary tree node.
5+
* type TreeNode struct {
6+
* Val int
7+
* Left *TreeNode
8+
* Right *TreeNode
9+
* }
10+
*/
11+
type TreeNode struct {
12+
Val int
13+
Left *TreeNode
14+
Right *TreeNode
15+
}
16+
17+
func sumOfLeftLeaves(root *TreeNode) int {
18+
if root == nil {
19+
return 0
20+
}
21+
left := sumOfLeftLeaves(root.Left)
22+
right := sumOfLeftLeaves(root.Right)
23+
// 是否是有效的左叶子节点
24+
var mid int
25+
if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
26+
mid = root.Left.Val
27+
}
28+
29+
return mid + left + right
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /