【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, 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].
Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
层次遍历,到每一层最后一个节点,即装入ret
每层最后一个节点的判断:
(1)队列为空
(2)下一个待遍历节点在下一层
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
struct Node
{
TreeNode* tnode;
int level; Node(TreeNode* t, int l): tnode(t), level(l) {}
}; class Solution {
public:
vector<int> rightSideView(TreeNode *root) {
vector<int> ret;
if(root == NULL)
return ret;
queue<Node*> q;
int curLevel = ;
Node* rootNode = new Node(root,);
q.push(rootNode); while(!q.empty())
{
Node* front = q.front();
q.pop(); if(q.empty() || q.front()->level > front->level)
//last node of current level
ret.push_back(front->tnode->val); if(front->tnode->left)
{
Node* leftNode = new Node(front->tnode->left, front->level+);
q.push(leftNode);
} if(front->tnode->right)
{
Node* rightNode = new Node(front->tnode->right, front->level+);
q.push(rightNode);
}
}
return ret;
}
};

【LeetCode】199. Binary Tree Right Side View的更多相关文章
- 【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】145. Binary Tree Postorder Traversal
		
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
 - 【LeetCode】Balanced Binary Tree 解题报告
		
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
 - 【LeetCode】Balanced Binary Tree(平衡二叉树)
		
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
 - 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
 - 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
 - 【LeetCode】Balanced Binary Tree 算法优化 解题报告
		
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
 - 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
 
随机推荐
- python2.7 调用 .net的webservice asmx
			
首先安装pip install suds 或下载:https://pypi.org/project/suds-jurko/0.6/#files 这个是最新版本 由于不支持python3.6, 所以只能 ...
 - TensorFlow (RNN)深度学习 双向LSTM(BiLSTM)+CRF 实现 sequence labeling 序列标注问题 源码下载
			
http://blog.csdn.net/scotfield_msn/article/details/60339415 在TensorFlow (RNN)深度学习下 双向LSTM(BiLSTM)+CR ...
 - Python防止sql注入
			
看了网上文章,说的都挺好的,给cursor.execute传递格式串和参数,就能防止注入,但是我写了代码,却死活跑不通,怀疑自己用了一个假的python 最后,发现原因可能是不同的数据库,对于字符串的 ...
 - Install Hyper-V on Windows 10
			
Enable Hyper-V to create virtual machines on Windows 10.Hyper-V can be enabled in many ways includ ...
 - ZH奶酪:PHP抓取网页方法总结
			
From:http://www.jb51.net/article/24343.htm 在做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过ht ...
 - JS获取当前/指定URL参数
			
方法: 首先通过 document.location 获得当前访问网页的网址, 其次用 split 方法通过“?”把网址分为两部分. 如果网址中有参数(arrObj.length > 1) 再用 ...
 - switch omega - VNP
			
文件下载 安装插件 1.Chrome地址栏输入:chrome://extensions/ 2.将附件拖入窗口,安装
 - SQL Server从BAK文件还原新的数据库
			
同一个数据库多个副本 很多时候,比如为了方便测试,排查问题,我们常常会拿到问题系统的数据库备份来开发环境下debug,这个时候就会出现同一个数据库的多个副本. 还原法 还原到一个新建的空数据库,在选项 ...
 - ldd 以及  ld-linux.so.2
			
最近跟编译工具干上了,可能是问题积累集中爆发的结果. 今天对 ld-linux.so.x 有很大兴趣,想对它多些了解,遂百度之.发现了指令 ldd. 关于 ldd 其实 ldd 是一个脚本,并不是一个 ...
 - Spring学习笔记六:Spring整合Hibernate
			
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6785323.html 前言:整合概述 Spring整合Hibernate主要是把Hibernate中常用的S ...