Skip to content

Latest commit

 

History

History
19 lines (17 loc) · 636 Bytes

103. 二叉树的锯齿形层序遍历.md

File metadata and controls

19 lines (17 loc) · 636 Bytes
class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        zigzagLevelOrderDFS(root, lists, 0);
        return lists;
    }

    private void zigzagLevelOrderDFS(TreeNode root, List<List<Integer>> lists, int deep) {
        if (root == null) return;
        if (lists.size() == deep) lists.add(new LinkedList<>());
        if (deep % 2 == 0) lists.get(deep).add(root.val);
        else lists.get(deep).add(0, root.val);
        zigzagLevelOrderDFS(root.left, lists, ++deep);
        zigzagLevelOrderDFS(root.right, lists, deep);
    }
}