【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 ...
随机推荐
- (转)最近一个项目中关于NGUI部分的总结(深度和drawCall)
在自己最近的一个项目中,软件的界面部分使用了NGUI来进行制作.在制作过程中,遇到了一些问题,也获取了一些经验,总结下来,作为日后的积累. 1.NGUI图集的使用. 此次是第一个自己正儿八经的制作完整 ...
- Struts2之server端验证
声明:在我的教程中有些东西,没有提及到.不是我不知道,而是在我个人来看对你们不是太重要的知识点.你们在看课本时有了解到即可.我不会面面俱到的都给你们提及.我写博文的目的是把我这一年的开发经验通过学习s ...
- Rainmeter 一部分 语法 中文教程
;Meter基本元素示例:;如果是[MeterStyle]表示Meter的公共Style,类似CSS的意义!!!;颜色可以使用网页的颜色定义方式,如半透明黄色:255.255.0.128=FFFF00 ...
- python 读取单所有json数据写入mongodb(单个)
<--------------主函数-------------------> from pymongo import MongoClientfrom bson.objectid impor ...
- activemq 控制面板里的 Number Of Pending Messages、 Messages Enqueued、Messages Dequeued含
Number Of Consumers 消费者 这个是消费者端的消费者数量 Number Of Pending Messages 等待消费的消息 这个是当前未出队列的数量.可以理解为总接收数-总出队 ...
- 树莓派通过GPIO控制步进电机
一.接线方式与GPIO调用方法: 电源接入+5v和GND In1-4分别接GPIO1-4 正转时,GPIO1-4分次传入:[1,0,0,0],[sleep],[0,1,0,0],[sleep],[0, ...
- JAVA 数据库连接池(伪代码,简单易读)
一.引言 近年来,随着 Internet/Intranet 建网技术的飞速发展和在世界范围内的迅速普及,电子商务的冲击波又一次在世界范围内掀起巨浪,各类商务网站吸引着大量用户的青睐,商务网站的访问量也 ...
- JAVA的Spring注入机制事例详解
一.前言 最近使用Spring里面的依赖注入,比如StudentServiceImple2.java代码: package di.service.imple; import com.mengya.sp ...
- 转:nginx基础概念(lingering_close)
lingering_close,字面意思就是延迟关闭,也就是说,当nginx要关闭连接时,并非立即关闭连接,而是再等待一段时间后才真正关掉连接.为什么要这样呢?我们先来看看这样一个场景.nginx在接 ...
- github使用入门
连接地址 github使用入门 连接地址: https://zhuanlan.zhihu.com/p/21193604?refer=passer