Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 102. Binary Tree Level Order Traversal #89

Merged
merged 1 commit into from
Feb 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading