mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-10-16 21:11:47 +00:00
添加Go语言题解
This commit is contained in:
@@ -34,7 +34,7 @@ class Solution {
|
||||
TreeNode cur = new TreeNode(-1);
|
||||
cur = root;
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
while (!stack.isEmpty() || cur != null) {
|
||||
while (!stack.isEmpty() || cur != null) {
|
||||
//找到当前应该遍历的那个节点
|
||||
while (cur != null) {
|
||||
stack.push(cur);
|
||||
@@ -47,7 +47,7 @@ class Solution {
|
||||
cur = temp.right;
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -78,4 +78,30 @@ 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
|
||||
}
|
||||
```
|
||||
|
||||
###
|
||||
|
@@ -426,6 +426,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)
|
||||
|
||||
大家如果吃透了二叉树的层序遍历的话,可以顺手把下面几道题目解决掉,思路一致,甚至都不用拐弯
|
||||
|
@@ -49,10 +49,10 @@ class Solution {
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
List<Integer> list = new ArrayList<>();
|
||||
Stack<TreeNode> stack = new Stack<>();
|
||||
if (root == null) return list;
|
||||
if (root == null) return list;
|
||||
stack.push(root);
|
||||
while (!stack.isEmpty()) {
|
||||
TreeNode temp = stack.pop();
|
||||
TreeNode temp = stack.pop();
|
||||
if (temp.right != null) {
|
||||
stack.push(temp.right);
|
||||
}
|
||||
@@ -95,3 +95,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
|
||||
}
|
||||
```
|
||||
|
@@ -12,7 +12,7 @@
|
||||
|
||||
我们知道后序遍历的顺序是,` 对于树中的某节点, 先遍历该节点的左子树, 再遍历其右子树, 最后遍历该节点`。
|
||||
|
||||
那么我们如何利用栈来解决呢?
|
||||
那么我们如何利用栈来解决呢?
|
||||
|
||||
我们直接来看动画,看动画之前,但是我们`需要带着问题看动画`,问题搞懂之后也就搞定了后序遍历。
|
||||
|
||||
@@ -104,6 +104,36 @@ 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)
|
||||
|
Reference in New Issue
Block a user