- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path623. Add One Row to Tree.go
42 lines (38 loc) · 852 Bytes
/
623. Add One Row to Tree.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
package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
typeTreeNode= structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
funcaddOneRow(root*TreeNode, vint, dint) *TreeNode {
ifd==1 {
tmp:=&TreeNode{Val: v, Left: root, Right: nil}
returntmp
}
level:=1
addTreeRow(root, v, d, &level)
returnroot
}
funcaddTreeRow(root*TreeNode, v, dint, currLevel*int) {
if*currLevel==d-1 {
root.Left=&TreeNode{Val: v, Left: root.Left, Right: nil}
root.Right=&TreeNode{Val: v, Left: nil, Right: root.Right}
return
}
*currLevel++
ifroot.Left!=nil {
addTreeRow(root.Left, v, d, currLevel)
}
ifroot.Right!=nil {
addTreeRow(root.Right, v, d, currLevel)
}
*currLevel--
}