[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 ...
随机推荐
- [UE4]C 语言动态数组
在实际的编程中,往往会发生这种情况,即所需的内存空间取决于实际输入的数据,而无法预先确定.对于这种问题,用静态数组的办法很难解决.为了解决上述问题,C语言提供了一些内存管理函数,这些内存管理函数结合指 ...
- 【BZOJ】1013 [JSOI2008]球形空间产生器sphere(高斯消元)
题目 传送门:QWQ 分析 高斯消元就是个大暴力.... 代码 #include <bits/stdc++.h> using namespace std; ; ; int n; doubl ...
- spark 存储管理机制
累加器 -- Accumulators 广播变量--Broadcast Variables 思考 回顾 存储管理模块架构--从架构上来看 存储管理模块架构--通信层 存储管理模块架构--存储层 存储管 ...
- 《opencv学习》 之 特征检测与匹配
这几天学习SURF特征检测,直接看的视频和书本有点吃不消,现在是基本看懂了,如果写博客记录没有必要,因为网上都差不多,笔记都在书上了,以下是个人认为比较浅显易懂的文章,当然海有很多好文章我没看到. 看 ...
- 可视化库-seaborn-单变量绘图(第五天)
1. sns.distplot 画直方图 import numpy as np import pandas as pd from scipy import stats, integrate impor ...
- ESCP打印机数据解密
通过串口调试工具 抓取到的16进制文本; 如下 然后打开我们的文档,查看命令数据内容. 详情请密我QQ:1161588342 说明加好友原因
- as3 文本竖排效果实现
import flash.text.engine.TextBlock; import flash.text.engine.ElementFormat; import flash.text.engine ...
- Springmvc Exception
对于异常处理,大多使用一个共同的异常类统一处理从dao,service,controller层抛出的异常,将页面跳转到共同的error页面. public class CommonException ...
- SecureCRT去除关闭Session的确认窗口提示
- Haskell语言学习笔记(32)Data.Maybe, Data.Either
Data.Maybe Prelude Data.Maybe> maybe "" show (Just 5) "5" Prelude Data.Maybe& ...