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 e72e215

Browse files
limingzhonglimingzhong
limingzhong
authored and
limingzhong
committed
fx
1 parent 066adca commit e72e215

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

‎src/tree/tree.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,32 @@ func MaxDepth(root *TreeNode) int {
191191

192192
return depth
193193
}
194+
195+
func PathSum(root *TreeNode, sum int) [][]int {
196+
var (
197+
res = make([][]int, 0)
198+
path = make([]int, 0)
199+
)
200+
201+
traversal(root, sum, path, &res)
202+
return res
203+
}
204+
205+
func traversal(root *TreeNode, sum int, list []int, res *[][]int) {
206+
if root == nil {
207+
return
208+
}
209+
list = append(list, root.Val)
210+
fmt.Println(list)
211+
fmt.Println(sum)
212+
if root.Left == nil && root.Right == nil {
213+
if sum == root.Val {
214+
*res = append(*res, list)
215+
}
216+
return
217+
}
218+
219+
traversal(root.Left, sum-root.Val, list, res)
220+
221+
traversal(root.Right, sum-root.Val, list, res)
222+
}

‎src/tree/tree_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,14 @@ func TestMaxDepth(t *testing.T) {
3636
root := PreOderDeserialize([]int{1, 2, -1, 4, -1, -1, 3, -1, -1})
3737
MaxDepth(root)
3838
}
39+
40+
// 1
41+
//2 3
42+
// 4
43+
44+
func TestPathSum(t *testing.T) {
45+
root := PreOderDeserialize([]int{5, 4, 11, 7, -1, -1, 2, -1, -1, 8, 13, -1, -1, 4, 5, -1, -1, 1, -1, -1})
46+
PreTraversal(root)
47+
rs := PathSum(root, 22)
48+
fmt.Println(rs)
49+
}

0 commit comments

Comments
(0)

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