My java solution using stack and queue


  • 0
    O
    public class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> result = new ArrayList<List<Integer>>(); 
        	if (root == null) {
        		return result;
        	}
            Queue<TreeNode> tmpQueue = new LinkedList<TreeNode>();
    		Stack<Queue<TreeNode>> stack = new Stack<Queue<TreeNode>>();
    		TreeNode tmp = root;
    		tmpQueue.add(tmp);
    		stack.push(tmpQueue);
    		while (!stack.isEmpty()) {
    			tmpQueue = stack.pop();
    	        Queue<TreeNode> newQueue = new LinkedList<TreeNode>();
    	    	List<Integer> list = new ArrayList<Integer>();
    			while (!tmpQueue.isEmpty()) {
    				tmp = tmpQueue.poll();
    				list.add(tmp.val);
    				if (tmp.left != null) {
    					newQueue.add(tmp.left);
    				}
    				if (tmp.right != null) {
    					newQueue.add(tmp.right);
    				}
    			}
    			if (!newQueue.isEmpty()) {
    				stack.push(newQueue);
    			}
    			result.add(list);
    		}
    		tmp = null;
    		tmpQueue = null;
            return result;
        }
    }

Log in to reply
 

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