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;
    }
}
相关推荐
s砚山s1 天前
代码随想录刷题——二叉树篇(十)
算法
2301_764441331 天前
基于HVNS算法和分类装载策略的仓储系统仿真平台
人工智能·算法·分类
AI科技星1 天前
统一场论变化的引力场产生电磁场推导与物理诠释
服务器·人工智能·科技·线性代数·算法·重构·生活
杰克逊的日记1 天前
规控算法(规划 + 控制算法)
大数据·算法·云计算·it
玉树临风ives1 天前
atcoder ABC439 题解
c++·算法
这周也會开心1 天前
JVM-垃圾回收算法
jvm·算法
学编程就要猛1 天前
算法:4.长度最小的子数组
算法
红豆诗人1 天前
算法和数据结构--时间复杂度和空间复杂度
数据结构·算法
高山上有一只小老虎1 天前
小红的字符串
java·算法