199. Binary Tree Right Side View (Tree, Stack)
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.
For example:
Given the following binary tree,
1 <---
/ \
2 3 <---
\ \
5 4 <---
You should return [1, 3, 4].
以下做法不对,还得考虑左边子树比右边子树长的部分
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> ret;
TreeNode* current = root;
while(current){
ret.push_back(current->val);
if(current->right) current = current->right;
else current = current->left;
}
return ret;
}
};
Result:Wrong Answer
Input: [1,2,3,4]
Output: [1,3]
Expected: [1,3,4]
所以,得用stack记录左边的子树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> ret;
TreeNode* current = root;
int depth = ; //depth so far
int depthGap;
stack<TreeNode*> nodeStack;
stack<int> depthStack; while(current){
ret.push_back(current->val);
depth++;
if(current->right){
if(current->left){
nodeStack.push(current->left);
depthStack.push(depth);
}
current = current->right;
}
else{
current = current->left;
}
} while(!nodeStack.empty()){
current = nodeStack.top();
nodeStack.pop();
depthGap = depth - depthStack.top();
depthStack.pop();
while(depthGap){
depthGap--;
if(current->right){
if(current->left){
nodeStack.push(current->left);
depthStack.push(depth - depthGap);
}
current = current->right;
}
else{
current = current->left;
}
if(!current) break;
}
if(!current) continue;
while(current){
ret.push_back(current->val);
depth++;
if(current->right){
if(current->left){
nodeStack.push(current->left);
depthStack.push(depth);
}
current = current->right;
}
else{
current = current->left;
}
}
}
return ret;
}
};
199. Binary Tree Right Side View (Tree, Stack)的更多相关文章
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)
Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...
- LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)
199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】199. Binary Tree Right Side View
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
- 【刷题-LeetCode】199 Binary Tree Right Side View
Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, ...
- [LeetCode] 199. Binary Tree Right Side View_ Medium tag: BFS, Amazon
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- 【LeetCode-面试算法经典-Java实现】【199-Binary Tree Right Side View(从右边看二叉树)】
[199-Binary Tree Right Side View(从右边看二叉树] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 代码下载[https://github.co ...
随机推荐
- 静态属性@property
property 作用其实把类里面的逻辑给隐藏起来(封装逻辑,让用户调用的时候感知不到你的逻辑) property实例1:class Room: def __init__(self): pass @p ...
- 利用STM32CubeMX来生成USB_HID_Mouse工程【添加ADC】(1)
现在原来的基础上添加ADC的功能. 现在(利用STM32CubeMX来生成USB_HID_Mouse工程)基础上新增硬件 JoyStick Shield 游戏摇杆扩展板 与STM32F103C8的连接 ...
- Xshell 用鼠标选中一段文字后自动换行
现象: 使用Xshell连接远程服务器,一般选中都是鼠标选中,然后按快捷键 Ctrl+Insert复制,Shift+Insert粘贴. 可是当选中后松开鼠标,这时候仿佛在xshell里自动输了一个回车 ...
- Runnable接口和Callable接口的区别。
Callable需要实现call方法,而Runnable需要实现run方法:并且,call方法还可以返回任何对象,无论是什么对象,JVM都会当作Object来处理.但是如果使用了泛型,我们就不用每次都 ...
- 注解(annotation)
目录 JAVA注解 SpringMVC注解 RestEasy注解 JSON注解 java注解 SpringMVC注解 restEasy注解 Json注解: @JsonInclude(JsonIncl ...
- using关键字在C#中的3种用法
using 关键字有两个主要用途: (一).作为指令,用于为命名空间创建别名或导入其他命名空间中定义的类型. (二).作为语句,用于定义一个范围,在此范围的末尾将释放对象. (一).作为指令 1. ...
- 大批量数据导出到Excel的实现
在平时的项目中,将数据导出到Excel的需求是很常见的,在此对一些常见的方法做以总结,并提供一种大数据量导出的实现. OLEDB 使用OLEDB可以很方便导出Excel,思路很简单,处理时将Exc ...
- RabbitMQ.Net 应用(2)
//生产者 using RabbitMQ.Client; using System; using System.Collections.Generic; using System.Linq; usin ...
- Python调用外部系统命令
利用Python调用外部系统命令的方法可以提高编码效率.调用外部系统命令完成后可以通过获取命令执行返回结果码.执行的输出结果进行进一步的处理.本文主要描述Python常见的调用外部系统命令的方法,包括 ...
- JDBC有哪些接口
1 实现Driver接口的对象是JDBC进行数据库访问的开始,可以通过java.lang.Class类的forName(),动态加载驱动程序. Class.forName("驱动程序&quo ...