Iterative solution in Go


  • 0
    W
    func inorderTraversal(root *TreeNode) []int {
        result := make([]int, 0)
        stack := make([]*TreeNode, 0)
        p := root
        for p != nil || len(stack) > 0 {
            for p != nil {
                stack = append(stack, p)
                p = p.Left
            }
            p = stack[len(stack) - 1]
            stack = stack[0:len(stack)-1]
            result = append(result, p.Val)
            p = p.Right
        }
        return result
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.