recursive java solution, 10 lines


  • 0
        public List<Integer> postorderTraversal(TreeNode root) {
            Deque<Integer> res = new ArrayDeque<>();
            Stack<TreeNode> s = new Stack<>();
            s.push(root);
            while(s.size() > 0) {
                TreeNode node = s.pop();
                if(node != null) {
                    res.addFirst(node.val);
                    s.push(node.left);
                    s.push(node.right);
                }
            }
            return new ArrayList<>(res);
        }
    

Log in to reply
 

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