-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 102. Binary Tree Level Order Traversal (#89)
Signed-off-by: ashing <[email protected]>
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
leetcode_daily/20240214/102. Binary Tree Level Order Traversal.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |