This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
forked from hoanhan101/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sum_path_test.go
80 lines (65 loc) · 1.91 KB
/
sum_path_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Problem:
- Given a binary tree where each node can only have a digit (0-9) value,
each root-to-leaf path will represent a number. Find the total sum of
all the numbers represented by all paths.
Example:
- Input:
1
2 3
4 5 6 7
Output: 522 (= 124 + 125 + 136 + 137)
Approach:
- Traverse the tree in a depth first search fashion.
- At each level, the sum is equal to the result of the last sum times 10
plus the current's node value.
Cost:
- O(n) time, O(n) space.
*/
package gtci
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestSumPath(t *testing.T) {
t1 := &common.TreeNode{Left: nil, Value: 1, Right: nil}
t1.Left = &common.TreeNode{Left: nil, Value: 2, Right: nil}
t1.Right = &common.TreeNode{Left: nil, Value: 3, Right: nil}
t1.Left.Left = &common.TreeNode{Left: nil, Value: 4, Right: nil}
t1.Left.Right = &common.TreeNode{Left: nil, Value: 5, Right: nil}
t1.Right.Left = &common.TreeNode{Left: nil, Value: 6, Right: nil}
t1.Right.Right = &common.TreeNode{Left: nil, Value: 7, Right: nil}
t2 := &common.TreeNode{Left: nil, Value: 1, Right: nil}
t2.Left = &common.TreeNode{Left: nil, Value: 2, Right: nil}
t2.Right = &common.TreeNode{Left: nil, Value: 3, Right: nil}
t2.Left.Left = &common.TreeNode{Left: nil, Value: 4, Right: nil}
t2.Right.Right = &common.TreeNode{Left: nil, Value: 7, Right: nil}
tests := []struct {
in *common.TreeNode
expected int
}{
{t1, 522},
{t2, 261},
}
for _, tt := range tests {
common.Equal(
t,
tt.expected,
sumPath(tt.in),
)
}
}
func sumPath(root *common.TreeNode) int {
return recurSumPath(root, 0)
}
func recurSumPath(node *common.TreeNode, sum int) int {
if node == nil {
return 0
}
sum = 10*sum + node.Value
// return the current sum if the current node is the leaf node.
if node.Left == nil && node.Right == nil {
return sum
}
return recurSumPath(node.Left, sum) + recurSumPath(node.Right, sum)
}