/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var zigzagLevelOrder = function(root) {
var result = [];
if (root == null) {
return result;
}
var queue = [];
queue.push(root);
var isOdd = true;
while (queue.length != 0) {
var size = queue.length;
var level = [];
for (var i = 0; i < size; i++) {
var node = queue.shift();
if (node.left != null) {
queue.push(node.left);
}
if (node.right != null) {
queue.push(node.right);
}
level.push(node.val);
}
if (isOdd == false) {
level.reverse();
}
result.push(level);
isOdd = !isOdd;
}
return result;
};
2016年8月23日星期二
[LeetCode]103. Binary Tree Zigzag Level Order Traversal
订阅:
博文评论 (Atom)
没有评论:
发表评论