diff --git a/leetcode_daily/20240214/102. Binary Tree Level Order Traversal.go b/leetcode_daily/20240214/102. Binary Tree Level Order Traversal.go new file mode 100644 index 0000000..78a70e0 --- /dev/null +++ b/leetcode_daily/20240214/102. Binary Tree Level Order Traversal.go @@ -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 +}