From fb44fd69a626b05dbd4e9bc59ec23b53d749a96d Mon Sep 17 00:00:00 2001 From: zouxinyao Date: Sat, 24 Jul 2021 17:32:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0GO=E8=AF=AD=E8=A8=80=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../二叉树/二叉树中序遍历(迭代).md | 26 +++++++++++++++ animation-simulation/二叉树/二叉树基础.md | 28 ++++++++++++++++ .../二叉树/二叉树的前序遍历(栈).md | 24 ++++++++++++++ .../二叉树/二叉树的后续遍历 (迭代).md | 32 +++++++++++++++++++ 4 files changed, 110 insertions(+) diff --git a/animation-simulation/二叉树/二叉树中序遍历(迭代).md b/animation-simulation/二叉树/二叉树中序遍历(迭代).md index 8dd15e7..8eb258e 100644 --- a/animation-simulation/二叉树/二叉树中序遍历(迭代).md +++ b/animation-simulation/二叉树/二叉树中序遍历(迭代).md @@ -78,5 +78,31 @@ class Solution { } ``` +Go Code: + +```go +func inorderTraversal(root *TreeNode) []int { + res := []int{} + if root == nil { + return res + } + stk := []*TreeNode{} + cur := root + for len(stk) != 0 || cur != nil { + // 找到当前应该遍历的那个节点,并且把左子节点都入栈 + for cur != nil { + stk = append(stk, cur) + cur = cur.Left + } + // 没有左子节点,则开始执行出栈操作 + temp := stk[len(stk) - 1] + stk = stk[: len(stk) - 1] + res = append(res, temp.Val) + cur = temp.Right + } + return res +} +``` + ### diff --git a/animation-simulation/二叉树/二叉树基础.md b/animation-simulation/二叉树/二叉树基础.md index 01a6f2c..493dac7 100644 --- a/animation-simulation/二叉树/二叉树基础.md +++ b/animation-simulation/二叉树/二叉树基础.md @@ -442,6 +442,34 @@ class Solution { } ``` +Go Code: + +```go +func levelOrder(root *TreeNode) [][]int { + res := [][]int{} + if root == nil { + return res + } + // 初始化队列时,记得把root节点加进去。 + que := []*TreeNode{root} + for len(que) != 0 { + t := []int{} + // 这里一定要先求出来que的长度,因为在迭代的过程中,que的长度是变化的。 + l := len(que) + for i := 0; i < l; i++ { + temp := que[0] + que = que[1:] + // 子节点不为空,就入队 + if temp.Left != nil { que = append(que, temp.Left) } + if temp.Right != nil { que = append(que, temp.Right) } + t = append(t, temp.Val) + } + res = append(res, t) + } + return res +} +``` + 时间复杂度:O(n) 空间复杂度:O(n) 大家如果吃透了二叉树的层序遍历的话,可以顺手把下面几道题目解决掉,思路一致,甚至都不用拐弯 diff --git a/animation-simulation/二叉树/二叉树的前序遍历(栈).md b/animation-simulation/二叉树/二叉树的前序遍历(栈).md index 90fea79..24242e1 100644 --- a/animation-simulation/二叉树/二叉树的前序遍历(栈).md +++ b/animation-simulation/二叉树/二叉树的前序遍历(栈).md @@ -96,3 +96,27 @@ class Solution { } ``` +Go Code: + +```go +func preorderTraversal(root *TreeNode) []int { + res := []int{} + if root == nil { + return res + } + stk := []*TreeNode{root} + for len(stk) != 0 { + temp := stk[len(stk) - 1] + stk = stk[: len(stk) - 1] + if temp.Right != nil { + stk = append(stk, temp.Right) + } + if temp.Left != nil { + stk = append(stk, temp.Left) + } + res = append(res, temp.Val) + } + return res +} +``` + diff --git a/animation-simulation/二叉树/二叉树的后续遍历 (迭代).md b/animation-simulation/二叉树/二叉树的后续遍历 (迭代).md index 5a8bac8..7881982 100644 --- a/animation-simulation/二叉树/二叉树的后续遍历 (迭代).md +++ b/animation-simulation/二叉树/二叉树的后续遍历 (迭代).md @@ -104,6 +104,38 @@ class Solution { } ``` +Go Code: + +```go +func postorderTraversal(root *TreeNode) []int { + res := []int{} + if root == nil { + return res + } + stk := []*TreeNode{} + cur := root + var pre *TreeNode + for len(stk) != 0 || cur != nil { + for cur != nil { + stk = append(stk, cur) + cur = cur.Left + } + // 这里符合本文最后的说法,使用先获取栈顶元素但是不弹出,根据栈顶元素的情况进行响应的处理。 + temp := stk[len(stk) - 1] + if temp.Right == nil || temp.Right == pre { + stk = stk[: len(stk) - 1] + res = append(res, temp.Val) + pre = temp + } else { + cur = temp.Right + } + } + return res +} +``` + + + 当然也可以修改下代码逻辑将 `cur = stack.pop()` 改成 `cur = stack.peek()`,下面再修改一两行代码也可以实现,这里这样写是方便动画模拟,大家可以随意发挥。 时间复杂度 O(n), 空间复杂度O(n)