Skip to content

Latest commit

 

History

History
27 lines (25 loc) · 557 Bytes

rightSideView.md

File metadata and controls

27 lines (25 loc) · 557 Bytes

二叉树的右视图

关键时,每层如果右树不存在的话,是要看到左树的。

var rightSideView = function(root) {
    if (!root) {
        return []
    }
    let ret = []
    let q = [root]
    while (q.length) {
        ret.push(q[0].val)
        let len = q.length
        while (len--) {
            let node = q.shift()
            if (node.right) {
                q.push(node.right)
            }
            if (node.left) {
                q.push(node.left)
            }
        }
    }
    return ret
};