Very Very simple JAVA solution beats 91%


  • 0
    R

    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
    List< List<Integer> > res=new ArrayList<List<Integer> >();
    levelDownOrder(res,root,0);
    return res;
    }
    public static void levelDownOrder(List< List<Integer>> res,TreeNode root, int level) {

             if(root==null) return;
            	
             
        	   if(res.size()<=level){
        		   List<Integer> tmp=new ArrayList<Integer>();
        		   tmp.add(root.val);
        		   res.add(tmp);
        	   }else
        		   if(level%2==1)  // simply add from the head if the level is even number...
        			  res.get(level).add(0,root.val);
        		   else
        		      res.get(level).add(root.val);  
                   
                	
    		   levelDownOrder(res, root.left, level+1);
        	   levelDownOrder(res, root.right, level+1);
    

    }


Log in to reply
 

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