JAVA Solution, 1ms, Simple and Clear

  • 0
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        helper(res, root, 1);
        for (int i = 0; i < res.size(); i++)
            if (i % 2 == 1)
        return res;
    private void helper(List<List<Integer>> res, TreeNode node, int level){
        if (node == null) return;
        if (res.size() < level) res.add(new ArrayList<Integer>());
        helper(res, node.left, level+1);
        helper(res, node.right, level+1);

Log in to reply

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