share my easy understand solution--Java_2ms


  • 0
    X

    public class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {

        List<List<Integer>> res= new ArrayList<List<Integer>>();
        
          if(root==null) return res;
         help(root,0,res);
         
         for(int i=0; i< res.size();i++){
             if (i%2!=0)  Collections.reverse(res.get(i));
         }
         
         return res;
         
    }
    
    public void help(TreeNode root, int height, List<List<Integer>> res){
        if(res.size()<=height) res.add(new ArrayList<Integer>());
        res.get(height).add(root.val);
        if(root.left!=null) help(root.left, height+1, res);
           if(root.right!=null) help(root.right, height+1, res);
        
    }
    

    }


Log in to reply
 

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