623. Add One Row to Tree
Last updated
Last updated
Input: root = [4,2,6,3,1,5], val = 1, depth = 2
Output: [4,1,1,2,null,null,6,3,1,5]func addOneRow(root *TreeNode, val int, depth int) *TreeNode {
if depth == 1 {
return &TreeNode{val, root, nil}
}
q := []*TreeNode{root}
for i := 0; i < depth-2 && len(q) > 0; i++ {
var p []*TreeNode
for j := 0; j < len(q); j++ {
if q[j].Left != nil {
p = append(p, q[j].Left)
}
if q[j].Right != nil {
p = append(p, q[j].Right)
}
}
q = p
}
for j := 0; j < len(q); j++ {
q[j].Left = &TreeNode{val, q[j].Left, nil}
q[j].Right = &TreeNode{val, nil, q[j].Right}
}
return root
}