Skip to content

Commit

Permalink
feat: 102. Binary Tree Level Order Traversal (#89)
Browse files Browse the repository at this point in the history
Signed-off-by: ashing <[email protected]>
  • Loading branch information
ronething authored Feb 14, 2024
1 parent a3cc870 commit 76639ff
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions leetcode_daily/20240214/102. Binary Tree Level Order Traversal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package _0240214

import "algorithm/template"

type TreeNode = template.TreeNode

func levelOrder(root *TreeNode) [][]int {
res := make([][]int, 0)
if root == nil {
return res
}

queue := []*TreeNode{root}
for len(queue) != 0 {
level := make([]int, 0)
size := len(queue)
for i := 0; i < size; i++ {
node := queue[0]
queue = queue[1:]
level = append(level, node.Val)
if node.Left != nil {
queue = append(queue, node.Left)
}
if node.Right != nil {
queue = append(queue, node.Right)
}
}
res = append(res, level)
}

return res
}

0 comments on commit 76639ff

Please sign in to comment.