/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type Stack struct {
Parr [1000]*TreeNode
Index int
}
func(s *Stack) IsEmpty() bool {
if s.Index <= 0 {
return true
}else {
return false
}
}
func(s *Stack) Push(p *TreeNode) {
s.Parr[s.Index] = p
s.Index = s.Index + 1
}
func(s *Stack) Pop() (*TreeNode){
if s.Index > 0 {
s.Index = s.Index - 1
return s.Parr[s.Index]
}else {
return nil
}
}
func postorderTraversal(root *TreeNode) []int {
rsp := make([]int, 0)
if nil == root {
return rsp
}
stack := Stack{}
stack.Push(root)
for ; !stack.IsEmpty(); {
temp := stack.Pop()
rsp = append(rsp, temp.Val)
if temp.Left != nil {
stack.Push(temp.Left)
}
if temp.Right != nil {
stack.Push(temp.Right)
}
}
rsp1 := make([]int, 0)
for i := len(rsp) - 1; i >= 0; i= i -1 {
rsp1 = append(rsp1, rsp[i])
}
return rsp1
}
03-29
623
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)