题解 | #按之字形顺序打印二叉树#

按之字形顺序打印二叉树

http://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0

//与层序遍历一样的模板,有些许地方变动,如下标注
function Print(root)
{
    // write code here
    var res=[],num=0;
    if(!root) return res
    var queue=[root];
//     queue.push(root);
    while(queue.length){
        var len=queue.length;
        var level=[];//存放每一层的节点
        num++;//表示层数
        for(var i=0;i<len;i++){
            var cur=queue.shift();
            level.push(cur.val)
            cur.left && queue.push(cur.left);
            cur.right && queue.push(cur.right);
        }
        if(num%2 == 0) level.reverse()  //判断是否是偶数层,是就反转该层的数组
        res.push(level)
    }
    return res
}

全部评论

相关推荐

07-02 10:44
门头沟学院 C++
码农索隆:太实诚了,告诉hr,你能实习至少6个月
点赞 评论 收藏
分享
06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
07-01 19:00
门头沟学院 Java
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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