From d8bbb1284d8122cff0d10fbb2149e6eee40e132f Mon Sep 17 00:00:00 2001 From: wurui Date: Thu, 11 Aug 2022 17:39:41 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86=E7=9A=84=E9=80=92?= =?UTF-8?q?=E5=BD=92=E9=81=8D=E5=8E=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) 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. 二叉树的层序遍历