add: 只出现一次的数字等

This commit is contained in:
2019-03-10 16:43:04 +08:00
committed by yi-ge
parent 930ff1be11
commit 8239e7afd5
106 changed files with 7978 additions and 0 deletions

View File

@ -0,0 +1,28 @@
/**
* 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
}