Merge pull request #1583 from woorui/master

添加 二叉树层序遍历的递归遍历
This commit is contained in:
程序员Carl
2022-08-13 10:18:58 +08:00
committed by GitHub

View File

@ -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. 二叉树的层序遍历