diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 5ef23944..9ad34494 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -205,6 +205,36 @@ class Solution { go: +```go +/** +102. 二叉树的递归遍历 + */ +func levelOrder(root *TreeNode) [][]int { + arr := [][]int{} + + depth := 0 + + var order func(root *TreeNode, depth int) + + order = func(root *TreeNode, depth int) { + if root == nil { + return + } + if len(arr) == depth { + arr = append(arr, []int{}) + } + arr[depth] = append(arr[depth], root.Val) + + order(root.Left, depth+1) + order(root.Right, depth+1) + } + + order(root, depth) + + return arr +} +``` + ```go /** 102. 二叉树的层序遍历