package main
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
    if root == nil {
        return nil
    }

    res := make([]int, 0)
    stack := []*TreeNode{root}

    for len(stack)>0 {
        top := stack[len(stack)-1]
        if top != nil {
            stack = stack[:len(stack)-1]

            if top.Right != nil {
                stack = append(stack, top.Right)
            }

            stack = append(stack, top, (*TreeNode)(nil))

            if top.Left != nil {
                stack = append(stack, top.Left)
            }
        }else{
            node := stack[len(stack)-2]
            res = append(res, node.Val)
            stack = stack[:len(stack)-2]
        }
    }

    return res
}