Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example:

Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation: 1 <---
/ \
2 3 <---
\ \
5 4 <--- Solution 1:
BFS
 /**
* 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<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root == null) {
return res;
}
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
if (i == 0) {
res.add(cur.val);
}
if (cur.right != null) {
queue.offer(cur.right);
}
if (cur.left != null) {
queue.offer(cur.left);
}
}
}
return res;
}
}

Solution 2:

DFS preOrder

/**
* 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;
}
helper(root, 0, res);
return res;
} private void helper(TreeNode root, int depth, List<Integer> res) {
if (root == null) {
return;
}
if (depth == res.size()) {
res.add(root.val);
}
helper(root.right, depth + 1, res);
helper(root.left, depth + 1, res);
}
}

[LC] 199. Binary Tree Right Side View的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  2. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  3. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  4. 【LeetCode】199. Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

  5. 【刷题-LeetCode】199 Binary Tree Right Side View

    Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...

  6. 199. Binary Tree Right Side View 从右侧看的节点数

    [抄题]: Given a binary tree, imagine yourself standing on the right side of it, return the values of t ...

  7. [leetcode]199. Binary Tree Right Side View二叉树右侧视角

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  8. [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  9. Java for LeetCode 199 Binary Tree Right Side View

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

随机推荐

  1. python语法基础-并发编程-线程-线程理论和线程的启动

    #######################       线程介绍         ############################## """ 线程介绍 为什 ...

  2. luffy课程表的创建-支付宝API-购买服务器

    课程组件 <template> <div class="course"> <Header></Header> <div cla ...

  3. Django整体架构

    Django整体架构 用户能够访问到的所有的资源 都是程序员提前暴露好的, 如果没有暴露 用户就永远访问不了 用户能够访问到的所有的资源 都是程序员提前暴露好的, 如果没有暴露 用户就永远访问不了 一 ...

  4. Golang解析json的几种方法

    Golang解析json的几种方法 概要 使用Golang调用其它平台API接口时总会被多层的json串给恶心到,我记录一下自己解析json的几种方法. 一.自带的json包 func JsonUnm ...

  5. 多标签图像分类任务的评价方法-mAP

    http://blog.sina.com.cn/s/blog_9db078090102whzw.html 多标签图像分类(Multi-label Image Classification)任务中图片的 ...

  6. 17.3.13---join函数

    1-----Python中有join()和os.path.join()两个函数,具体作用如下:     join():    连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成 ...

  7. 01 语言基础+高级:1-4 接口与多态_day10【接口、多态】&&day11【final、匿名内部类】

    day10[接口.多态] 接口三大特征——多态引用类型转换 教学目标写出定义接口的格式写出实现接口的格式说出接口中成员的特点能够说出使用多态的前提条件理解多态的向上转型理解多态的向下转型 day10_ ...

  8. C# 使用 HttpPost 请求调用 WebService (转)

    转自 https://www.cnblogs.com/Brambling/p/7266482.html 之前调用 WebService 都是直接添加服务引用,然后调用 WebService 方法的,最 ...

  9. apache启动失败,提示80端口被占用

    首先检查80端口被什么程序占用,方法:cmd进DOS,输入netstat -ano 80端口被为4的进程占用,有两种可能:一种情况是本机中安装了sqlserver 2008,80端口被SqlServe ...

  10. 再来看看Java8的新特征——lambda表达式

    什么是lambda表达式? 可以把Lambda表达式理解为简洁地表示可传递的匿名函数的一种方式:它没有名称,但它有参数列表.函数主体.返回类型,可能还有一个可以抛出的异常列表. 比如说new一个Thr ...