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 ...
随机推荐
- ASP.NET Core MVC 概述
https://docs.microsoft.com/zh-cn/aspnet/core/mvc/overview?view=aspnetcore-2.2 ASP.NET Core MVC 概述 20 ...
- CKEditor 5
1.官网 https://ckeditor.com/ckeditor-5/download/ 2.
- sql server 语法 MSDN
https://docs.microsoft.com/en-us/sql/t-sql/statements/create-view-transact-sql
- 人脸识别68个点<转>
[Opencv] 于仕琪 人脸68个特征点分布情况 // 鼻尖 30 // 鼻根 27 // 下巴 8 // 左眼外角 36 // 左眼内角 39 // 右眼外角 45 // 右眼内角 42 // 嘴 ...
- Model操作补充
参考: http://www.cnblogs.com/wupeiqi/articles/6216618.html
- Python运算符,基本数据类型
1,基本的运算符: 加,减,乘,除 取余(%) 取商(//) **(幂) in not in (判断是否在里面) 1.运算符 结果是值 算数运算 ...
- C# 如何利用反射,将字符串转化为类名并调用类中方法
首先,先随便创建一个测试类 <span style="font-family:Microsoft YaHei;font-size:18px;">public class ...
- 尚硅谷redis学习4-数据类型
redis的数据类型包括String,Hash(类似于JAVA里的map),List,Set,Zset(sorted Set) String(字符串) string是redis最基本的类型,你可以理解 ...
- 【365】拉格朗日乘子法与KKT条件说明
参考:知乎回答 - 通过山头形象描述 参考:马同学 - 如何理解拉格朗日乘子法? 参考: 马同学 - 如何理解拉格朗日乘子法和KKT条件? 参考:拉格朗日乘数 - Wikipedia 自己总结的规律 ...
- SQL Server 数据库中的几个常见的临界值
本文出处:http://www.cnblogs.com/wy123/p/6709520.html 1,SQL语句或者存储过程的最大长度(SQL字符串容量)是多少? 经常有人问,我的SQL语句是拼凑出来 ...