forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
47 lines (47 loc) · 890 Bytes
/
Solution.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
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func replaceValueInTree(root *TreeNode) *TreeNode {
s := []int{}
var dfs1 func(*TreeNode, int)
dfs1 = func(root *TreeNode, depth int) {
if root == nil {
return
}
if len(s) <= depth {
s = append(s, 0)
}
s[depth] += root.Val
dfs1(root.Left, depth+1)
dfs1(root.Right, depth+1)
}
var dfs2 func(*TreeNode, int)
dfs2 = func(root *TreeNode, depth int) {
l, r := 0, 0
if root.Left != nil {
l = root.Left.Val
}
if root.Right != nil {
r = root.Right.Val
}
sub := l + r
depth++
if root.Left != nil {
root.Left.Val = s[depth] - sub
dfs2(root.Left, depth)
}
if root.Right != nil {
root.Right.Val = s[depth] - sub
dfs2(root.Right, depth)
}
}
dfs1(root, 0)
root.Val = 0
dfs2(root, 0)
return root
}