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. Python自动生成100以内加减乘除混合运算题

    import random from random import choice ops = ('+','-','×','÷') ans = [] i=0 while i < 100 : op1 ...

  2. Mysql 常用函数(2)- if 函数

    Mysql常用函数的汇总,可看下面系列文章 https://www.cnblogs.com/poloyy/category/1765164.html if 的作用 根据表达式的某个条件或值结果来执行一 ...

  3. SpringBoot 整合SpringBatch实际项目改造

    SpringBoot整合SpringBatch项目,已将代码开源至github,访问地址:https://github.com/cmlbeliever/SpringBatch 欢迎star or fo ...

  4. vue 路由钩子。

    一.全局钩子 你可以使用 router.beforeEach 注册一个全局的 before 钩子: const router = new VueRouter({ ... }) router.befor ...

  5. PAT 1002 A+B for Polynomials (25分)

    题目 This time, you are supposed to find A+B where A and B are two polynomials. Input Specification: E ...

  6. nginx操作目录

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

  7. 关于mysql的metadata lock

    昨天晚上上线,却发现一个ddl语句长时间没有生效 查processlist, 发现包括ddl语句在内的众多查询提示 “Waiting for table metadata lock” 唯一没有该提示的 ...

  8. logback如何配置springboot框架

    创建logback-spring.xm在src/main/resources下面(springboot推荐使用logback-spring.xml而不是logback.xml)文件. logback- ...

  9. VMware如何克隆一个虚拟机

    如何在Vmware克隆一个虚拟机,并修改哪些配置. 克隆虚拟机步骤 其中模板虚拟机的安装部署可参见:「VMware安装Linux CentOS 7.7系统」 找到克隆的模板机,并选择克隆. 进入克隆虚 ...

  10. MYSQL(1)— 初识MySQL

    一.MySQL [官网文档]https://dev.mysql.com/doc/refman/8.0/en/ 1-1.Mysql是一个关系型数据库,里面的表数据是可以有联系的,例如多对一,一对多. 1 ...