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)的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  2. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  3. Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View)

    Leetcode之深度优先搜索(DFS)专题-199. 二叉树的右视图(Binary Tree Right Side View) 深度优先搜索的解题详细介绍,点击 给定一棵二叉树,想象自己站在它的右侧 ...

  4. LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

    199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Giv ...

  5. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  6. 【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, ...

  7. 【刷题-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, ...

  8. [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 ...

  9. 【LeetCode-面试算法经典-Java实现】【199-Binary Tree Right Side View(从右边看二叉树)】

    [199-Binary Tree Right Side View(从右边看二叉树] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 代码下载[https://github.co ...

随机推荐

  1. [AS3]as3中splice和slice的用法介绍说明

    splice 删除数组一段连续的元素,返回被删除的元素数组 var arr:Array = ["a","b","c","d&quo ...

  2. UI5-学习篇-6-SAP创建OData服务-RFC

    1.创建项目 2.Import RFC接口 3.定义实体名 目标服务器:若连接外部服务器则需SM59配置Destination 选择RFC函数名 4.选择数据源参数 5.设置主键值 6.保存成功 7. ...

  3. spring-mvc.xml与spring-mybatis.xml配置文件中命名空间问题

    首先贴出配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...

  4. include 模板标签

    {%load staticfiles %}就能使用include标签了 {% include %}该标签允许在(模板中)包含其他的模板的内容,标签的参数是所要包含的模板名称,可以是一个变量,也可以是用 ...

  5. 服务限流-令牌桶java实现

    此文非常不错,抄自: https://www.cnblogs.com/googlemeoften/p/6020718.html 其他实现 https://www.cnblogs.com/LBSer/p ...

  6. vue项目动态控制数据变动时箭头样式

    html代码 <div class="top_precent"> <span :class="{arrow:numPrecent<0}" ...

  7. Eclipse代码自动补全

    Eclipse自动补全方法 Window -> preferences -> Java -> Editor -> Content assist -> Auto-Activ ...

  8. HTML+CSS基础课程三

    1.文字排版--字体 我们可以使用css样式为网页中的文字设置字体.字号.颜色等样式属性.下面我们来看一个例子,下面代码实现:为网页中的文字设置字体为宋体. body{font-family:&quo ...

  9. easyui中的几个问题

    easyui中的tree,采用url参数读取json,无法显示.有可能是vs的IIS不支持,$.ajax 原因待测试,有知道的朋友也可以贴代码,我解决的一个办法是 $(function () { $. ...

  10. nagiosQL访问时报错PHP message: PHP Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead

    nagiosQL安装环境: CentOS release 6.4 (Final) php-5.5.4 nagiosql_320 nginx version: nginx/1.2.3 安装一切正常,当访 ...