Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Apr 16, 2024
1 parent d942e77 commit d9a73a9
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions leetcode/623.AddOneRowtoTree/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// https://leetcode.com/problems/add-one-row-to-tree/

package main

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func addOneRow(root *TreeNode, val int, depth int) *TreeNode {
if depth == 1 {
return &TreeNode{val, root, nil}
}
q := []*TreeNode{root}
i := 0
for len(q) > 0 {
i++
for k := len(q); k > 0; k-- {
node := q[0]
q = q[1:]
if node.Left != nil {
q = append(q, node.Left)
}
if node.Right != nil {
q = append(q, node.Right)
}
if i == depth-1 {
node.Left = &TreeNode{val, node.Left, nil}
node.Right = &TreeNode{val, nil, node.Right}
}
}
}
return root
}

0 comments on commit d9a73a9

Please sign in to comment.