mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加 二叉树层序遍历的递归遍历
This commit is contained in:
@ -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. 二叉树的层序遍历
|
||||||
|
Reference in New Issue
Block a user