题解 | #从上往下打印二叉树#

从上往下打印二叉树

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
};

#我的实习求职记录#
全部评论

相关推荐

点赞 评论 收藏
分享
牛客ID:561366855:期望薪资多少?难以相信这简历找不到工作。说明二本电子信息专业想对口就业非常难。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务