golang栈方法,使用list实现栈
package main
import "container/list"
import . "nc_tools"
/*
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
func inorderTraversal( root *TreeNode ) []int {
// write code here
res:=make([]int, 0)
stack:=list.New()
for root!=nil || stack.Len()!=0{
for root!=nil{
stack.PushBack(root)
root=root.Left
}
node:=stack.Remove(stack.Back()).(*TreeNode)
res=append(res, node.Val)
root=node.Right
}
return res
}

京公网安备 11010502036488号