题解 | #把二叉树打印成多行#

把二叉树打印成多行

http://www.nowcoder.com/practice/445c44d982d04483b04a54f298796288

就只是增加了一个确认每一层有几个节点的for循环

/* function TreeNode(x) {
    this.val = x;
    this.left = null;
    this.right = null;
} */
function Print(pRoot)
{
    // write code here
    let res=[]
    let quene=[]
    quene.push(pRoot)
    while(quene[0]){
        let tmp=[];
        for(let i=quene.length;i>0;i--){
            let node=quene.shift()
            tmp.push(node.val)
            if(node.left) quene.push(node.left)
            if(node.right) quene.push(node.right)
        }
        res.push(tmp)
    }
    return res
}
module.exports = {
    Print : Print
};
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务