/**
* 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 inorderTraversal(root *TreeNode) []int {
rsp := make([]int, 0)
if nil == root {
return rsp
}
stack := Stack{}
for ; !stack.IsEmpty() || root != nil; {
for ; root != nil; {
stack.Push(root)
root = root.Left
}
if !stack.IsEmpty() {
root = stack.Pop()
rsp = append(rsp, root.Val)
root = root.Right
}
}
return rsp
}
01-12
410
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-11
1794
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-06
1608
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交