A C# Classic solution


  • 0
    L
    public IList<int> PostorderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<TreeNode>();
        IList<int> result = new List<int>();
        for(TreeNode lastNode = null, peek; root != null || stack.Count != 0;)
            if(root != null){
                stack.Push(root);
                root = root.left;
            }
            else if((peek = stack.Peek()).right != null && peek.right != lastNode)
                root = peek.right;
            else{
                result.Add(peek.val);
                lastNode = stack.Pop();
            }
        return result;
    }

Log in to reply
 

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