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 <---

题意:

给定一个二叉树,

想象自己站在二叉树的右侧

返回所有能看到的node(返回的node的order是从上往下)

思路:

dfs,

使其访问顺序为: root --> right --> left

这样访问nodes为:10, 15, 20, 12, 5, 7, 9, 2

用一个level来维护每层关系,  便于把每层最右侧nodes取出来,放入result中

则:(10,0), (15,1), (20,2), (12,2), (5,1), (7,2), (9,3), (2,2)

每次一到达新的level,根据root --> right --> left的访问顺序,该新level最先被访问的node一定是最右侧view 需要输出的node。将其加入result中。

(10,0), (15,1), (20,2), (12,2), (5,1), (7,2), (9,3), (2,2)

result输出: 10, 15, 20, 9

代码:

 public class BinaryTreeRightSideView {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<>();
// corner case
if (root == null) return result;
dfs(root, result, 0);
return result;
} public void dfs(TreeNode root, List<Integer> result, int level) {
//recursion 的base case
if (root == null) {
return;
}
if (level == result.size()) {
result.add(root.val);
} dfs(root.right, result, level + 1);
dfs(root.left, result, level + 1); }
}

思路

BFS(iteration)

每次先将right side node 加入到queue里去

保证 当i = 0 的时候,poll出来的第一个item是right side node

代码

 1 public List<Integer> rightSideView(TreeNode root) {
2 // level order traversal
3 List<Integer> result = new ArrayList();
4 Queue<TreeNode> queue = new LinkedList();
5 // corner case
6 if (root == null) return result;
7
8 queue.offer(root);
9 while (!queue.isEmpty()) {
10 int size = queue.size();
11 for (int i = 0; i< size; i++) {
12 TreeNode cur = queue.poll();
13 // make sure only add right side node
14 if (i == 0) result.add(cur.val);
15 // add right side node first, making sure poll out first
16 if (cur.right != null) queue.offer(cur.right);
17 if (cur.left != null) queue.offer(cur.left);
18 }
19 }
20 return result;
21 }


[leetcode]199. Binary Tree Right Side View二叉树右侧视角的更多相关文章

  1. [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 ...

  2. [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 ...

  3. leetcode 199 :Binary Tree Right Side View

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

  4. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  5. (二叉树 bfs) 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 ...

  6. 199 Binary Tree Right Side View 二叉树的右视图

    给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树,   1            <--- /   \2     3         <--- \  ...

  7. leetcode@ [199] Binary Tree Right Side View (DFS/BFS)

    https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...

  8. LeetCode OJ: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. Dubbo各种协议

    协议参考手册 (+) (#) 推荐使用Dubbo协议 性能测试报告各协议的性能情况,请参见:性能测试报告 (+) dubbo:// (+) (#) Dubbo缺省协议采用单一长连接和NIO异步通讯,适 ...

  2. width:100%和width:auto区别

    在div父元素是body时 1.先看没有width限制的div <div style="border:1px solid red; margin-left:50px; margin-r ...

  3. jps命令发生异常

    当在集群里输入jps命令时报如下错误: 我就开始检查jdk,感觉应该是centos自动的jdk没卸载干净跟后面安装的jdk冲突 先通过命令 rpm -qa|grep java 查看jdk信息 把这几个 ...

  4. 蓝瓶的钙,好喝的钙——windows,我要蓝屏的

    原文地址:http://80x86.io/post/windows-blue-screen-0x00000050-page_fault_in_nonpaged_area 这里只截取一部分. windo ...

  5. java在注解中绑定方法参数的解决方案

    我们有这样子的需求,需要记录用户操作某个方法的信息并记录到日志里面,例如,用户在保存和更新任务的时候,我们需要记录下用户的ip,具体是保存还是更新,调用的是哪个方法,保存和更新的任务名称以及操作是否成 ...

  6. opencv查看源代码

    这一节是一个插曲,有的人刚开始学opencv就看源代码,有的人直接拿着opencv的API用...... 学了一个多月opencv了,就是没找到源代码,想看的时候都是从网上找的,或者看网上说从哪个文件 ...

  7. js url转码

    JS中对URL进行转码与解码   1. escape 和 unescape escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值. 采用unicode字符集对指定的 ...

  8. 修改IP

    查看系统版本 [root@host ~]# cat /etc/issueCentOS release 6.5 (Final)Kernel \r on an \m [root@host ~]# cat ...

  9. spring事务没回滚

    最近遇见一个问题,用spring管理实务,在service层处理数据,保存数据时出现异常,但没有回滚,检查了一下,发现是因为我用try catch将异常进行捕获了,没有抛出导致的:默认spring事务 ...

  10. H5 缓存机制解析

    在web项目开发中,我们可能都曾碰到过这样一个棘手的问题: 线上项目需要更新一个有问题的资源(可能是图片,js,css,json数据等),这个资源已经发布了很长一段时间,为什么页面在浏览器里打开还是没 ...