Untitled
unknown
plain_text
a year ago
991 B
2
Indexable
class Solution { public List<List<Integer>> zigzagLevelOrder(TreeNode root) { List<List<Integer>> result = new ArrayList<List<Integer>>(); if (root == null) { return result; } Queue<TreeNode> q = new LinkedList<TreeNode>(); q.add(root); int rowIndex = 0; while (!q.isEmpty()) { List<Integer> rows = new ArrayList<Integer>(); int size = q.size(); for (int i = 0; i < size; i++) { TreeNode current = q.poll(); rows.add(current.val); if (current.left != null) { q.add(current.left); } if (current.right != null) { q.add(current.right); } } if (rowIndex % 2 == 1) { Collections.reverse(rows); } result.add(rows); rowIndex++; } return result; } }
Editor is loading...
Leave a Comment