标签:rsa pre tree node eve for evel while turn ++i
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> ans = new ArrayList<>();
if (root == null) return ans;
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
int cnt = 0;
while (!q.isEmpty()) {
List<Integer> l = new ArrayList<>();
int sz = q.size();
for (int i = 0; i < sz; ++i) {
TreeNode t = q.poll();
if (cnt % 2 == 0) {
l.add(t.val);
} else {
l.add(0, t.val);
}
if (t.left != null) q.add(t.left);
if (t.right != null) q.add(t.right);
}
ans.add(l);
cnt++;
}
return ans;
}
}
标签:rsa pre tree node eve for evel while turn ++i
原文地址:https://www.cnblogs.com/clown9804/p/12643831.html