题解 | #按之字形顺序打印二叉树#
按之字形顺序打印二叉树
http://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function Print(pRoot)
{
// write code here
if(pRoot == null) return [];
const queue=[pRoot];
let depth=0;
const res=[];
while(queue.length>0){
let level=[];
let size=queue.length;
while(size--){
const cur=queue.shift();
depth % 2 === 0 ?level.push(cur.val):level.unshift(cur.val);
if(cur.left){
queue.push(cur.left);
}
if(cur.right){
queue.push(cur.right);
}
}
depth++;
res.push(level);
}
return res;
}
module.exports = {
Print : Print
};