LeetCode——199. 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:
输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:
1 <---
/ \
2 3 <---
\ \
5 4 <---
https://leetcode-cn.com/problems/binary-tree-right-side-view/
递归 DFS
递归方法是分别遍历一个节点的右节点和左节点,因为是从右边看过来,所以我们需要首先遍历右节点。
这里有个疑问,当遍历左节点时候,怎么判定它右边没有其他节点了呢?
这里我们用到一个变量level,对于同一层的节点,如果res数组的大小已经等于level了,说明右边已经有节点存入数组了,该节点就不用再保存。一直递归下去就可以得到结果。
C++
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> res;
helper(root,0,res);
return res;
}
void helper(TreeNode* root,int level,vector<int>& res){
if(!root) return;
if(res.size()==level) res.push_back(root->val);
helper(root->right,level+1,res);
helper(root->left,level+1,res);
}
};
非递归 BFS
这道题要求我们打印出二叉树每一行最右边的一个数字,实际上是求二叉树层序遍历的一种变形,我们只需要保存每一层最右边的数字即可,还是需要用到数据结构队列queue,遍历每层的节点时,把下一层的节点都存入到queue中,每当开始新一层节点的遍历之前,先把新一层最后一个节点值存到结果中,代码如下:
C++
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
res.push_back(q.back()->val);
int size = q.size();
for(int i=0; i<size; ++i){
TreeNode* t=q.front(); q.pop();
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
}
return res;
}
};
java
class Solution {
public List<Integer> rightSideView(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
List<Integer> ret = new ArrayList<>();
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
if (i == size - 1) {
ret.add(node.val);
}
if (node.left != null) {
queue.add(node.left);
}
if (node.right !=null) {
queue.add(node.right);
}
}
}
return ret;
}
}
python
class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
if not root: return []
res = []
def bfs(root):
queue = [root]
while queue:
nxt = []
res.append(queue[-1].val)
for node in queue:
if node.left:
nxt.append(node.left)
if node.right:
nxt.append(node.right)
queue = nxt
bfs(root)
return res
LeetCode——199. 二叉树的右视图的更多相关文章
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...
- Java实现 LeetCode 199 二叉树的右视图
199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, ...
- 力扣Leetcode 199. 二叉树的右视图
199. 二叉树的右视图 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 ...
- leetcode.199二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4]输出: [1, 3, 4]解释: 1 <-- ...
- LeetCode 199 二叉树的右视图
题目: 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 ...
- LeetCode 199. 二叉树的右视图 C++ 用时超100%
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- 领扣(LeetCode)二叉树的右视图 个人题解
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. 示例: 输入: [1,2,3,null,5,null,4] 输出: [1, 3, 4] 解释: 1 < ...
随机推荐
- hibernate注解 笔记
1.hibernate使用@where实现条件过滤功能 其里面只有一个参数clause,完整用法是: @Where(clause = "VALID_FLAG=1") 可以加在实体类 ...
- 外部 Storage Provider【转】
如果 Kubernetes 部署在诸如 AWS.GCE.Azure 等公有云上,可以直接使用云硬盘作为 Volume,下面是 AWS Elastic Block Store 的例子: 要在 Pod 中 ...
- windows driver 获取本地时间
#define ArrayLength 260 void MyGetLocalTime() { LARGE_INTEGER li_system; LARGE_INTEGER li_Local; cha ...
- openstack trove实例状态转换条件--Mitaka版本
今天研究了一下trove的instance状态转换条件.发现其实设计的挺复杂的.后来思考了一下这样设计的原因.我觉得大致是因为,如果instance的状态全部来自于instance上跑的数据库服务本身 ...
- Swift轮播控件快速入门——FSPagerView
2018年03月01日 19:17:42 https://blog.csdn.net/sinat_21886795/article/details/79416068 今天介绍一个IOS的轮播控件FSP ...
- MongoDB三-高级操作
复制来自:http://www.cnblogs.com/huangxincheng/archive/2012/02/21/2361205.html 今天跟大家分享一下mongodb中比较好玩的知识,主 ...
- ORACLE增删改查以及case when的基本用法
1.创建table create table test01( id int not null primary key, name ) not null, gender ) not null, age ...
- Python说文解字_杂谈09
1. 元类编程代码分析: import numbers class Field: pass class IntField(Field): # 数据描述符: # 初始化 def __init__(sel ...
- .NET httpClient Post请求,GET请求方法
1.后端是WebAPI,POST请求,修饰符是[FromBody]的字符串,[FromBody]修饰的时候数据是来自body部分,而不是来自url部分,所以后端取值会自动映射出数据,比如后端是这样的, ...
- 根据pdf文件获取标题等信息
根据 kdd2019的 pdf文件, 生成索引文档. 代码如下: for fname in ` ls pdfs/*.pdf`; do title=$(mdls -name kMDItemTitle - ...