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. [UE4]acotor放置4*4列表

    // Number of blocks const int32 NumBlocks = Size * Size; // Loop to spawn each block ; BlockIndex< ...

  2. 不重启修改'log_slave_updates'变量

    Variable 'log_slave_updates' is a read only variable 不重启修改mysql变量 执行复制的时候遇到的问题 mysql> show variab ...

  3. Quectel module USB driver for linux

    The environment settings are as follows: 1. ubuntu 14.04 , linux kernel: linux-lts-xenial-4.4.0 2. m ...

  4. CSS3 圆角属性 border-radius和-webkit-border-radius使用

    CSS3 圆角属性 border-radius 在 CSS3 中新增了一个 border-radius 边框半径属性,即大家常用的圆角效果.这使得制作圆角将不再麻烦,只需对所用对象加一个 border ...

  5. json 拖拽

    1.梳理知识点 1.事件对象   e || event  2.事件对象的属性      鼠标事件对象 : 坐标属性 :  clientX  clientY  pageX  pageY   offset ...

  6. Notepad++配置c++编译环境

    博主学生狗一只,之前一直进行.net开发,用惯了微软的那一套.C#上手容易,开发起来简单,但是正如前辈们所说的,它隐藏了太多底层的东西,惯坏了我,导致快毕业了有些东西一无所知.同时自己想写点简单的算法 ...

  7. hbase权限管理

    给用户分配对每个表的操作权限,有RWXCA五种,对应READ, WRITE, EXEC, CREATE, ADMIN hbase(main):222:0> help "grant&qu ...

  8. centos配置DNS和ip

    Centos6.5 永久修改DNS地址的方法 1.配置ip地址文件 /etc/sysconfig/network-scripts/ifcfg-eth0添加一行 DNS1=8.8.8.8    #手动添 ...

  9. shift 参数移位

    更改批处理文件中可替换参数的位置. SHIFT [/n] 如果命令扩展名被启用,SHIFT 命令支持/n 命令行开关:该命令行开关告诉命令从第 n 个参数开始移位:n 介于零和八之间.例如: SHIF ...

  10. net send 换行和发送广播

    net send ip message 要换行的时候按ctrl+t.最后按enter 或 ctrl + m 发出 如果是批处理里面要用: 在命令行下使用:echo ^T > a.txt,注意这里 ...