[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 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二叉树右侧视角的更多相关文章
- [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 ...
- [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 ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- (二叉树 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 ...
- 199 Binary Tree Right Side View 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树, 1 <--- / \2 3 <--- \ ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- unity3d的碰撞检测及trigger
A.基本概念 要产生碰撞必须为游戏对象添加刚体(Rigidbody)和碰撞器,刚体可以让物体在物理影响下运动.碰撞体是物理组件的一类,它要与刚体一起添加到游戏对象上才能触发碰撞.如果两个刚体相互撞在一 ...
- Neuromation新研究:利用卷积神经网络进行儿童骨龄评估
近日,Neuromation 团队在 Medium 上撰文介绍其最新研究成果:利用卷积神经网络(CNN)评估儿童骨龄,这一自动骨龄评估系统可以得到与放射科专家相似或更好的结果.该团队评估了手骨不同区域 ...
- msq_table's methods2
-- 删除数据 自增长id被占用 -- 清楚所有数据并重置id 1 truncate table name; -- 主键(唯一) id int primary key; -- 主键内容不能重复,不能为 ...
- js url转码
JS中对URL进行转码与解码 1. escape 和 unescape escape()不能直接用于URL编码,它的真正作用是返回一个字符的Unicode编码值. 采用unicode字符集对指定的 ...
- CUDA C Programming Guide 在线教程学习笔记 Part 9
▶ 协作组,要求 cuda ≥ 9.0,一个简单的例子见 http://www.cnblogs.com/cuancuancuanhao/p/7881093.html ● 灵活调节需要进行通讯的线程组合 ...
- jquery 的 $.extend 和 $.fn.extend
$.extend({ add:function(a,b){return a+b;}, bad:function(a,b){return a-b;} }); $.fn.extend({ loading: ...
- 子元素scroll父元素容器不跟随滚动JS实现
仅供参考: function parentNotRoll($id){ var flg;//标记滚动方向,true-向下,false-向上 var $test = document.getElement ...
- ABAP-FTP通用操作
- Java synchronized(this)锁住的是什么
synchronized锁住的是括号里面的对象,而不是代码. 对于非static的synchronized方法,锁的就是对象本身,也就是this.
- Java多线程对同一个对象进行操作
示例: 三个窗口同时出售20张票. 程序分析: 1.票数要使用一个静态的值. 2.为保证不会出现卖出同一张票,要使用同步锁. 3.设计思路:创建一个站台类Station,继承THread,重写run方 ...