My AC Java, beats 96%


  • -2
    D

    public static List<List<Integer>> zigzagLevelOrder(TreeNode root){
    List<List<Integer>> list = new ArrayList<List<Integer>>();
    helper(list,root,0,true);
    return list;
    }
    private static void helper(List<List<Integer>> list, TreeNode root, int height, boolean b) {
    if(root==null)
    return;
    if(height>=list.size()){
    list.add(new ArrayList<Integer>());
    }
    if(b){
    list.get(height).add(root.val);
    }else{
    list.get(height).add(0, root.val);
    }
    helper(list,root.left,height+1,!b);
    helper(list,root.right,height+1,!b);
    }


Log in to reply
 

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