Java Solution


  • 0
    V

    class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
    ArrayList<Integer> list = new ArrayList<Integer>();
    if (root == null) return list;
    Stack<TreeNode> stack = new Stack<TreeNode>();
    stack.push(root);

        while(!stack.isEmpty()) {
            TreeNode node = stack.pop();
            list.add(node.val);
            
            if (node.left != null) {
                stack.push(node.left);
            }
            
            if (node.right != null) {
                stack.push(node.right);
            }
        }
        
        Collections.reverse(list);
        return list;
    }
    

    }


Log in to reply
 

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