# 【算法】队列

1. 滑动窗口的平均值

``````/**
* Initialize your data structure here.
* @param {number} size
*/
var MovingAverage = function(size) {
this.size = size;
this.arr = []
this.sum = 0
};

/**
* @param {number} val
* @return {number}
*/
MovingAverage.prototype.next = function(val) {
this.arr.push(val)
this.sum += val
if(this.arr.length>this.size) {
this.sum -= this.arr.shift()
}
return this.sum / this.arr.length
};``````
1. 最近请求次数

``````var RecentCounter = function() {
this.arr = []
};

/**
* @param {number} t
* @return {number}
*/
RecentCounter.prototype.ping = function(t) {
this.arr.push(t)
while(this.arr.length > 1 && this.arr[0] + 3000 < t) {
this.arr.shift()
}
return this.arr.length
};``````

## 二叉树的广度优先遍历

1. 在完全二叉树中添加节点

``````/**
* @param {TreeNode} root
*/
var CBTInserter = function(root) {
if(root == null) return [];
this.root = root;
this.queue = [];
this.queue.push(root)
while(this.queue[0].left&&this.queue[0].right) {
const node = this.queue.shift();
this.queue.push(node.left)
this.queue.push(node.right)
}
};

/**
* @param {number} v
* @return {number}
*/
CBTInserter.prototype.insert = function(v) {
const parent = this.queue[0]
const child = new TreeNode(v)
if(parent.left == null) {
parent.left = child
} else {
parent.right = child;
this.queue.shift()
this.queue.push(parent.left)
this.queue.push(parent.right)
}
return parent.val;
};

/**
* @return {TreeNode}
*/
CBTInserter.prototype.get_root = function() {
return this.root
};
``````

1. 二叉树中每层的最大值

``````
/**
* @param {TreeNode} root
* @return {number[]}
*/
var largestValues = function(root) {
let current = 0;
let next = 0;
const queue = [];
if(root != null) {
queue.push(root)
current = 1
}

const result = [] // 结果
let max = -Infinity;
while(queue.length) {
const node = queue.shift();
current--
max = Math.max(max, node.val)

if(node.left != null) {
queue.push(node.left)
next++
}

if(node.right != null) {
queue.push(node.right)
next++
}

if(current === 0) {
result.push(max)
current = next
max = -Infinity
next = 0
}
}
return result
};``````

• 双队列实现广度优先搜索
1. 二叉树最低层最左边的值

``````
/**
* @param {TreeNode} root
* @return {number}
*/
var findBottomLeftValue = function(root) {
let queue = []
let next_queue = []
queue.push(root)
let temp = []
while(queue.length) {
const node = queue.shift()
temp.push(node)
if(node.left) {
next_queue.push(node.left)
}
if(node.right) {
next_queue.push(node.right)
}

if(queue.length == 0 && next_queue.length) {
queue = next_queue
temp = []
next_queue = []
}
}
return temp[0].val
};``````
1. 二叉树的右侧视图

``````/**
* @param {TreeNode} root
* @return {number[]}
*/
var rightSideView = function(root) {
if(root == null) return []
let queue = []
let next_queue = []
queue.push(root)
let temp = []
const result = []
while(queue.length) {
const node = queue.shift()
temp.push(node)
if(node.left) {
next_queue.push(node.left)
}
if(node.right) {
next_queue.push(node.right)
}

if(queue.length == 0 && next_queue.length) {
queue = next_queue
result.push(temp.pop().val)
temp = []
next_queue = []
}
}
if(temp.length) {
result.push(temp.pop().val)
}
return result;
};``````

1 篇内容引用
##### 看见了

i'm look for a good job :)

621 声望
0 粉丝
0 条评论