添加 二叉树层序遍历的递归遍历

This commit is contained in:
wurui
2022-08-11 17:39:41 +08:00
parent 4fbecbc1c1
commit d8bbb1284d

View File

@ -205,6 +205,36 @@ class Solution {
go: 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 ```go
/** /**
102. 二叉树的层序遍历 102. 二叉树的层序遍历