1、基础知识
【剑指offer2】 chap2 数组_飞行模式yu的博客-CSDN博客
参考LinkedList函数,均可实现
Queue:
常用函数
add——offer
remove——poll
element——peek
接口:
LinkedList
ArrayDeque
PriorityQueue
Deque(双端队列)
2、基本题型
(1)先入先出
滑动窗口
class MovingAverage {
private Queue<Integer> nums;
private int capacity;
private int sum;
public MovingAverage(int size) {
nums = new LinkedList<>();
capacity = size;
}
public double next(int val) {
nums.offer(val);
sum += val;
if (nums.size()>capacity) {
sum -= nums.poll();
}
return (double) sum / nums.size();
}
}
剑指 Offer II 042. 最近请求次数 while(队首)
class RecentCounter {
private Queue<Integer> times;
private int windowSize;
public RecentCounter() {
times = new LinkedList<>();
windowSize = 3000;
}
public int ping(int t) {
times.offer(t);
while (times.peek() + windowSize < t){
times.poll();
}
return times.size();
}
}
(2)二叉树BFS
单队列+res
//bfs算法
public List<Integer> bfs(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
if (root != null) queue.offer(root);
List<Integer> result = new ArrayList<>();
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
result.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
return result;
}
二叉搜索树模板
class CBTInserter {
private Queue<TreeNode> queue;
private TreeNode root;
public CBTInserter(TreeNode root) {
this.root = root;
queue = new LinkedList<>();
queue.offer(root);
while (queue.peek().left != null && queue.peek().right != null) {
TreeNode node = queue.poll();
queue.offer(node.left);
queue.offer(node.right);
}
}
public int insert(int v) {
TreeNode parent = queue.peek();
TreeNode node = new TreeNode(v);
if (parent.left == null){
parent.left = node;
}else {
parent.right = node;
queue.poll();
queue.offer(parent.left);
queue.offer(parent.right);
}
return parent.val;
}
public TreeNode get_root() {
return this.root;
}
}
分层输出
计数法 或者 双队列交替法(加一个queue1是否为空的判断)
(1)计数法
//方法一:计数法
public List<Integer> largestValues(TreeNode root) {
int curr = 0;
int next = 0;
Queue<TreeNode> queue = new LinkedList<>();
List<Integer> result = new LinkedList<>();
if (root != null) {
queue.offer(root);
curr++;
}
int max = Integer.MIN_VALUE;
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
curr--;
max = Math.max(max, node.val);
if (node.left != null) {
queue.offer(node.left);
next++;
}
if (node.right != null) {
queue.offer(node.right);
next++;
}
if (curr == 0) {
result.add(max);
curr = next;
max = Integer.MIN_VALUE;
next = 0;
}
}
return result;
}
(2)双队列交替法
//方法二:双队列交替
public List<Integer> largestValues2(TreeNode root) {
Queue<TreeNode> queue1 = new LinkedList<>();
Queue<TreeNode> queue2 = new LinkedList<>();
List<Integer> result = new LinkedList<>();
if (root != null) {
queue1.offer(root);
}
int max = Integer.MIN_VALUE;
while (!queue1.isEmpty()) {
TreeNode node = queue1.poll();
max = Math.max(max,node.val);
if (node.left != null) {
queue2.offer(node.left);
}
if (node.right != null) {
queue2.offer(node.right);
}
if (queue1.isEmpty()) {
queue1 = queue2;
queue2 = new LinkedList<>();
result.add(max);
max = Integer.MIN_VALUE;
}
}
return result;
}
剑指 Offer II 045. 二叉树最底层最左边的值 每层第一个
if (queue1.isEmpty()) {
queue1 = queue2;
queue2 = new LinkedList<>();
if(!queue1.isEmpty()){
bottomLeft = queue1.peek().val;
}
}
剑指 Offer II 046. 二叉树的右侧视图 每层最后一个
if (queue1.isEmpty()) {
result.add(node.val);
queue1 = queue2;
queue2 = new LinkedList<>();
}