题解 | #从上往下打印二叉树#
从上往下打印二叉树
https://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701
/* function TreeNode(x) {
this.val = x;
this.left = null;
this.right = null;
} */
function PrintFromTopToBottom(root)
{
// write code here
let queue = [];
let res = [];
if(root == null)
return [];
queue.push(root);
while(queue.length != 0){
// let temp = queue[0];
let temp = queue.shift();
res.push(temp.val);
if(temp.left){
queue.push(temp.left)
}
if(temp.right){
queue.push(temp.right)
}
}
return res
}
module.exports = {
PrintFromTopToBottom : PrintFromTopToBottom
};
#我的实习求职记录#