js-practice/src/tree/binary-tree-right-side-view.js

29 lines
778 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
export const rightSideView = function (root) {
if (!root) return []
const queue = [root] // 队列 把树顶加入队列
const arr = [] // 用来存储每层最后个元素值
while (queue.length > 0) {
let len = queue.length
while (len) {
const node = queue.shift() // 取出队列第一个元素
if (len === 1) arr.push(node.val) // 当是 当前一层的最后一个元素时把值加入arr
if (node.left) queue.push(node.left) // 继续往队列添加元素
if (node.right) queue.push(node.right)
len--
}
}
return arr
}