剑指 Offer II 队列

041. 滑动窗口的平均值

Queue queue = new LinkedList();
循环队列实现

class MovingAverage {
        int hh=0,tt=0;
        double sum=0;
          int []q;
    /** Initialize your data structure here. */
    public MovingAverage(int size) {
        q=new int [size];
    }
    //循环队列
    public double next(int val) {
        tt=(tt+1)%q.length;    
        if(hh==tt)
        {
            sum-=q[hh]; 
              hh=(hh+1)%q.length;
            sum+=q[tt]=val;
            return sum/q.length;
        }
        else   sum+=q[tt]=val;
        return sum/(tt-hh);
    
    }
}

042. 最近请求次数

class RecentCounter {
Queue<Integer> queue;
    public RecentCounter() {
        queue = new LinkedList<Integer>();

    }   
    public int ping(int t) {
          queue.offer(t);
          while(queue.peek()<t-3000)queue.poll();
          return queue.size();

    }
}

044. 二叉树每层的最大值

class Solution {

    public List<Integer> largestValues(TreeNode root) {
       
        List<Integer>ans=new ArrayList<>();
         if(root==null)return ans;
        Queue<TreeNode>q=new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty())
        {
            int n=q.size();
            int res=Integer.MIN_VALUE;
            for(int i=0;i<n;i++)//处理一层
            {
                TreeNode t=q.poll();
                 TreeNode l=t.left,r=t.right;
                 if(l!=null)q.offer(l);
                  if(r!=null)q.offer(r);
                res=Math.max(res,t.val);
            }
            ans.add(res);
        }
        return ans;
    }
}

045. 二叉树最底层最左边的值

dfs

class Solution {
    int res=0,step=0;
    void dfs(TreeNode x,int depth)
    {
        if(depth>step)
        {
            step=depth;
            res=x.val;
        }
        TreeNode p=x.left;
        TreeNode q=x.right;
        if(p!=null)dfs(p,depth+1);
        if(q!=null)dfs(q,depth+1);

    }
    public int findBottomLeftValue(TreeNode root) {
      dfs(root,1);
      return res;
    }
}

BFS


class Solution {
    public int findBottomLeftValue(TreeNode root) {
       Queue<TreeNode>q=new LinkedList<>();
       q.offer(root);
     int res=0;
      while(!q.isEmpty())
       { 
           res=q.peek().val;
           int n=q.size();
           for(int i=0;i<n;i++)//下一层全弹出来
           {
               TreeNode x=q.poll();
               if(x.left!=null)q.offer(x.left);
                if(x.right!=null)q.offer(x.right);
           }
       }
       return res;
     

    }
}

046. 二叉树的右侧视图

class Solution { 
    List<Integer> ans=new ArrayList<>();
    public List<Integer> rightSideView(TreeNode root) {
        if(root==null)return ans;
        Queue <TreeNode> q=new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty())
        {
            int n=q.size();
            int temp=0;
            for(int i=0;i<n;i++)
            {
                TreeNode t=q.poll();
                TreeNode l=t.left; TreeNode r=t.right;
                if(l!=null)q.offer(l);
                if(r!=null)q.offer(r);
                temp=t.val;//最右边会一直更新嘛
            }
            ans.add(temp);
        }
        return ans;
    }
}
posted @ 2022-03-06 23:07  liv_vil  阅读(32)  评论(0)    收藏  举报