二叉树 添加GO语言代码
parent
e35a85a38b
commit
fb44fd69a6
|
@ -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
|
||||
}
|
||||
```
|
||||
|
||||
###
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
大家如果吃透了二叉树的层序遍历的话,可以顺手把下面几道题目解决掉,思路一致,甚至都不用拐弯
|
||||
|
|
|
@ -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
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue