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

从上往下打印二叉树

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

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

相关推荐

04-13 18:10
门头沟学院 Java
想熬夜的小飞象在秋招:被腾讯挂了后爸妈以为我失联了
点赞 评论 收藏
分享
06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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