Slightly Modified from Preorder Traverse


  • 0
    S
    public class Solution {
        public List<Integer> postorderTraversal(TreeNode root) {
            List<Integer> res = new LinkedList<>();
            if(root==null) return res;
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while(!stack.empty()){
                TreeNode node = stack.pop();
                res.add(0, node.val);
                if(node.left!=null) stack.push(node.left);
                if(node.right!=null) stack.push(node.right);
            }
            return res;
        }
    }

Log in to reply
 

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