day55(1.6)——leetcode面试经典150

199. 二叉树的右视图

199. 二叉树的右视图

题目:

题解:

java 复制代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        //用bfs
        List<Integer> list = new LinkedList<>();
        if(root == null) {
            return list;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(queue.size()>0) {
            int size = queue.size();
            for(int i=0;i<size;i++) {
                TreeNode node = queue.poll();
                if(i==size-1) {
                    list.add(node.val);
                }
                if(node.left != null) {
                    queue.add(node.left);
                }
                if(node.right != null) {
                    queue.add(node.right);
                }
            }
        }
        return list;
    }
}
相关推荐
ysa05103016 分钟前
【板子】短序列dp(换成维护更小常数维度的dp)
c++·笔记·算法·板子
shwill12318 分钟前
PID 算法(三)--- 增量 PID ↔ 单神经元 PID 等价映射
linux·算法
孓最求完美35 分钟前
Charles 进阶抓包教程:突破 SSL Pinning、代理检测与 Flutter 抓包限制
面试
thesky1234561 小时前
智能体面试准备(九):工作流编排——DAG 与状态机两种范式的取舍与手写实现
面试·agent·状态机·工作流·智能体
wabs6662 小时前
关于图论【卡码网110.字符串迁移的思考】
数据结构·算法·图论
hanlin032 小时前
刷题笔记:力扣第242、349题(哈希表)
笔记·算法·leetcode
Revolution612 小时前
reactive 对象重新赋值后,表单为什么没有恢复默认值
前端·vue.js·面试
HeiSenBerg2 小时前
Android Handler 机制完全解析:从源码到内存泄漏,一篇就够了
面试
only-qi3 小时前
大模型Agent面试攻略:落地工程痛点、评估体系与Agentic RAG核心精讲
人工智能·算法·面试·职场和发展·langchain·rag
ShineWinsu4 小时前
对于Linux:传输层协议UDP原理的解析
linux·c++·面试·udp·协议·传输层·计算机系统