It is my right code.


  • 0
    L

    this is my code. Accepted.

    	List<List<Integer>> result = new  ArrayList<List<Integer>>();
    	if (root == null) {
    		return result;
    	}
    	
    	List<List<Integer>> alal 	= 	new ArrayList<List<Integer>>();
    	List<Integer> 				al 	= 	new ArrayList<Integer>();
    	
    	Queue<TreeNode> queue = new LinkedList<TreeNode>();
    	queue.add(root);
        int currentLevelNum =   1;      // number of current level 
        int nextLevelNum    =   0;      // number of next level
    
    	
    	while(!queue.isEmpty()){			
    			TreeNode curNode = queue.remove();
    			currentLevelNum--;
    			al.add(curNode.val);
    			if (curNode.left != null) {
    				queue.add(curNode.left);
    				nextLevelNum++;
    			} 
    			if (curNode.right != null) {
    				queue.add(curNode.right);
    				nextLevelNum++;
    			}
    			
    			if (currentLevelNum == 0) {
    				alal.add(al);
    				al = new ArrayList<Integer>();
    				currentLevelNum = 	nextLevelNum;
    				nextLevelNum 	=	0;
    			}				
    	}
    	
    	result = alal;
    	return result;

Log in to reply
 

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