199. 二叉树的右视图

给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

示例:

输入: [1,2,3,null,5,null,4]

输出: [1, 3, 4]

解释:

   1            <---
/ \
2 3 <---
\ \
5 4 <---

PS:

1层序遍历

2递归

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root == null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
int count = queue.size();
while(count > 0){
count--;
TreeNode cur = queue.poll();
if(count == 0){
//只有上一层的最后一个才能加入res
//如果右面有,就是右面
//右面没有,左面就是上一层的最后一个
res.add(cur.val);
}
//先加左面,先poll左面
if(cur.left != null){
queue.add(cur.left);
}
if(cur.right != null){
queue.add(cur.right);
}
}
}
return res;
} }
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int[] max = {0};
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
helper(res, root, 1);
return res;
} private void helper(List<Integer> res,TreeNode treeNode, int deep) {
if (treeNode == null) {
return;
}
if (deep > max[0]) {
max[0] = deep;
res.add(treeNode.val);
}
helper(res, treeNode.right, deep + 1);
helper(res, treeNode.left, deep + 1);
}
}

Java实现 LeetCode 199 二叉树的右视图的更多相关文章

  1. LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

    199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...

  2. 力扣Leetcode 199. 二叉树的右视图

    199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, ...

  3. LeetCode——199. 二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 < ...

  4. LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

    题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 ...

  5. leetcode.199二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4]输出: [1, 3, 4]解释: 1 <-- ...

  6. LeetCode 199 二叉树的右视图

    题目: 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 ...

  7. LeetCode 199. 二叉树的右视图 C++ 用时超100%

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  8. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

  9. 领扣(LeetCode)二叉树的右视图 个人题解

    给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 < ...

随机推荐

  1. JavaScript 的核心机制——event loop(最易懂版)

    前言 javascript从诞生之日起就是一门单线程的非阻塞的脚本语言. 非阻塞就是当代码需要进行一项异步任务(无法立刻返回结果,需要花一定时间才能返回的任务,如ajax事件)时,主线程会挂起(pen ...

  2. 【hdu5100】棋盘覆盖

    http://acm.hdu.edu.cn/showproblem.php?pid=5100 题目大意: 用1*k的木块铺n*n的棋盘,求多铺满多少个单位格. 方法: n < k,显然无解:n ...

  3. 疑问_网址参数不同的时候改如何选择_MySQL_芬兰站

    豹子安全-注入工具-疑问_网址参数不同的时候改如何选择_MySQL_芬兰站_kouvolankipparit.fi_基于联合查询_20200416 www.leosec.net 请看下列GIF视频:

  4. CSS:必须要掌握的重要基础知识点

    目录 1. 盒子 2. 常用选择器 3. 优先级 4. CSS继承 5. 伪元素(pseudo-element)和伪类(pseudo-class) 6. CSS:元素定位机制(positioning ...

  5. linux-Curl error (37): Couldn't read a file:// file for file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-x86_64 [Couldn't open file /e tc/pki/rpm-gpg/RPM-GPG-KEY-fedora-x86_64]

    在安装Python3-pip 的时候遇到 [root@localhost rpm-gpg]# yum install python3-pipFedora 31 - x86_64 - Updates - ...

  6. 对background: url("~assets/img/common/collect.svg") 0 0/14px 14px 的理解

    需求:给收藏数字前面通过::before伪元素添加图标 相关代码: .goods-info .collect { position: relative; } .goods-info .collect: ...

  7. 朴实的聊聊很多人会误解/不懂的Java并发中断机制

    | 好看请赞,养成习惯 你有一个思想,我有一个思想,我们交换后,一个人就有两个思想 If you can NOT explain it simply, you do NOT understand it ...

  8. 【雕爷学编程】Arduino动手做(49)---有源蜂鸣器模块

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践(动手试试)出真知的理念,以学习和交流为目的,这里准备 ...

  9. python之PyCharm下载和安装教程

    PyCharm 是 JetBrains 公司(www.jetbrains.com)研发,用于开发 Python 的 IDE 开发工具.图 1 所示为 JetBrains 公司开发的多款开发工具,其中很 ...

  10. nginx操作目录

    nginx配置文件/conf/nginx.conf 错误日志功能:los/error.log 访问日志功能:logs/access.log 站点服务请求功能配置:html/ 禁止访问功能配置 404页 ...