mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
更新 golang 二叉树遍历的所有代码,置换为和 c++ 模板相同的逻辑,而不是新的代码
之前的 golang 代码很不美观,并且他的逻辑和 c++ 的不一样,谁还专门记两个模板啊, 并且连缩进也没有,所以我就重写了一份 现在的 golang 逻辑和 c++ 相同 并且也有缩进,也没有废话代码,舒服多了
This commit is contained in:
@ -299,108 +299,96 @@ class Solution:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
> 迭代法前序遍历
|
> 迭代法前序遍历
|
||||||
|
|
||||||
```go
|
```go
|
||||||
//迭代法前序遍历
|
|
||||||
/**
|
|
||||||
type Element struct {
|
|
||||||
// 元素保管的值
|
|
||||||
Value interface{}
|
|
||||||
// 内含隐藏或非导出字段
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *List) Back() *Element
|
|
||||||
前序遍历:中左右
|
|
||||||
压栈顺序:右左中
|
|
||||||
**/
|
|
||||||
func preorderTraversal(root *TreeNode) []int {
|
func preorderTraversal(root *TreeNode) []int {
|
||||||
|
ans := []int{}
|
||||||
|
|
||||||
if root == nil {
|
if root == nil {
|
||||||
return nil
|
return ans
|
||||||
}
|
}
|
||||||
var stack = list.New()
|
|
||||||
stack.PushBack(root.Right)
|
st := list.New()
|
||||||
stack.PushBack(root.Left)
|
st.PushBack(root)
|
||||||
res:=[]int{}
|
|
||||||
res=append(res,root.Val)
|
for st.Len() > 0 {
|
||||||
for stack.Len()>0 {
|
node := st.Remove(st.Back()).(*TreeNode)
|
||||||
e:=stack.Back()
|
|
||||||
stack.Remove(e)
|
ans = append(ans, node.Val)
|
||||||
node := e.Value.(*TreeNode)//e是Element类型,其值为e.Value.由于Value为接口,所以要断言
|
if node.Right != nil {
|
||||||
if node==nil{
|
st.PushBack(node.Right)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
res=append(res,node.Val)
|
if node.Left != nil {
|
||||||
stack.PushBack(node.Right)
|
st.PushBack(node.Left)
|
||||||
stack.PushBack(node.Left)
|
|
||||||
}
|
}
|
||||||
return res
|
}
|
||||||
|
return ans
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
> 迭代法后序遍历
|
> 迭代法后序遍历
|
||||||
|
|
||||||
```go
|
```go
|
||||||
//迭代法后序遍历
|
|
||||||
//后续遍历:左右中
|
|
||||||
//压栈顺序:中右左(按照前序遍历思路),再反转结果数组
|
|
||||||
func postorderTraversal(root *TreeNode) []int {
|
func postorderTraversal(root *TreeNode) []int {
|
||||||
|
ans := []int{}
|
||||||
|
|
||||||
if root == nil {
|
if root == nil {
|
||||||
return nil
|
return ans
|
||||||
}
|
}
|
||||||
var stack = list.New()
|
|
||||||
stack.PushBack(root.Left)
|
st := list.New()
|
||||||
stack.PushBack(root.Right)
|
st.PushBack(root)
|
||||||
res:=[]int{}
|
|
||||||
res=append(res,root.Val)
|
for st.Len() > 0 {
|
||||||
for stack.Len()>0 {
|
node := st.Remove(st.Back()).(*TreeNode)
|
||||||
e:=stack.Back()
|
|
||||||
stack.Remove(e)
|
ans = append(ans, node.Val)
|
||||||
node := e.Value.(*TreeNode)//e是Element类型,其值为e.Value.由于Value为接口,所以要断言
|
if node.Left != nil {
|
||||||
if node==nil{
|
st.PushBack(node.Left)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
res=append(res,node.Val)
|
if node.Right != nil {
|
||||||
stack.PushBack(node.Left)
|
st.PushBack(node.Right)
|
||||||
stack.PushBack(node.Right)
|
|
||||||
}
|
}
|
||||||
for i:=0;i<len(res)/2;i++{
|
|
||||||
res[i],res[len(res)-i-1] = res[len(res)-i-1],res[i]
|
|
||||||
}
|
}
|
||||||
return res
|
reverse(ans)
|
||||||
|
return ans
|
||||||
|
}
|
||||||
|
|
||||||
|
func reverse(a []int) {
|
||||||
|
l, r := 0, len(a) - 1
|
||||||
|
for l < r {
|
||||||
|
a[l], a[r] = a[r], a[l]
|
||||||
|
l, r = l+1, r-1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
> 迭代法中序遍历
|
> 迭代法中序遍历
|
||||||
|
|
||||||
```go
|
```go
|
||||||
//迭代法中序遍历
|
|
||||||
func inorderTraversal(root *TreeNode) []int {
|
func inorderTraversal(root *TreeNode) []int {
|
||||||
rootRes:=[]int{}
|
ans := []int{}
|
||||||
if root==nil{
|
if root == nil {
|
||||||
return nil
|
return ans
|
||||||
}
|
}
|
||||||
stack:=list.New()
|
|
||||||
node:=root
|
st := list.New()
|
||||||
//先将所有左节点找到,加入栈中
|
cur := root
|
||||||
for node!=nil{
|
|
||||||
stack.PushBack(node)
|
for cur != nil || st.Len() > 0 {
|
||||||
node=node.Left
|
if cur != nil {
|
||||||
}
|
st.PushBack(cur)
|
||||||
//其次对栈中的每个节点先弹出加入到结果集中,再找到该节点的右节点的所有左节点加入栈中
|
cur = cur.Left
|
||||||
for stack.Len()>0{
|
} else {
|
||||||
e:=stack.Back()
|
cur = st.Remove(st.Back()).(*TreeNode)
|
||||||
node:=e.Value.(*TreeNode)
|
ans = append(ans, cur.Val)
|
||||||
stack.Remove(e)
|
cur = cur.Right
|
||||||
//找到该节点的右节点,再搜索他的所有左节点加入栈中
|
|
||||||
rootRes=append(rootRes,node.Val)
|
|
||||||
node=node.Right
|
|
||||||
for node!=nil{
|
|
||||||
stack.PushBack(node)
|
|
||||||
node=node.Left
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rootRes
|
|
||||||
|
return ans
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user