Readable Iterable Java code for Post Order Traversal using Deque


  • 0
    S
    public class Solution {
             public List<Integer> postorderTraversal(TreeNode root) {
                     List<Integer> list = new ArrayList<Integer>();
                     if(root == null)
                            return list;        
                     Deque<TreeNode> q = new LinkedList<TreeNode>();
                     q.addLast(root);        
                     while(q.size() > 0 ) {            
                            TreeNode cur = q.removeLast();
                            list.add(0,cur.val);            
                            if(cur.left != null)
                                 q.addLast(cur.left);
                            if(cur.right != null)
                                 q.addLast(cur.right);            
                     }
                    return list;  
           }
    }
    

Log in to reply
 

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