【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 ...
随机推荐
- fn:replace()函数
fn:replace()函数将字符串中所有指定的子串用另外的字符串替换. 语法 fn:replace()函数的语法如下: ${fn:replace(<原始字符串>, <被替换的字符串 ...
- linux命令学习——md5sum
1.简介 MD5算法常常被用来验证网络文件传输的完整性,防止文件被人篡改.MD5 全称是报文摘要算法(Message-Digest Algorithm 5),此算法对任意长度的信息逐位进行计算,产生一 ...
- 数据科学家Docker历险记(1):windows下环境搭建
原文:http://www.xueqing.tv/cms/article/247 Docker是最近炒得很火热的一门技术,在网上可以找到关于它的介绍文章,比如<Docker到底是什么?为什么它这 ...
- Spring Integration概述
1. Spring Integration概述 1.1 背景 Spring框架的一个重要主题是控制反转.从广义上来说,Spring处理其上下文中管理的组件的职责.只要组件减轻了职责,它们同 ...
- Discuz常见小问题-_如何修改网站LOGO
1 界面-风格管理,点击编辑 2 默认的LOGO图片叫做logo.png,存放在X:\PHPNow\htdocs\Discuz\upload\static\image\common文件夹下面,其中X是 ...
- 面试小结之Elasticsearch篇
https://www.cnblogs.com/luckcs/articles/7052932.html
- 【转】使用 Android 的日志工具LogCat
Android中的日志工具类是 Log(android.util.Log),这个类中提供了如下几个方法来供我们打印日志. 1. Log.v() 这个方法用于打印那些最为琐碎的,意义最小的日志信息 ...
- iOS界面篇 - bounds和frame的相同和区别
相同点: 他们都是CGRect类型,且拥有属性origin(x, y), size(weight, height) 不同点: bounds是你画的视图的边界,和父视图没有半毛钱关系 frames则一 ...
- 1073: 动物简介(animal)
#include<iostream> #include<string> #include<stdio.h> #include<algorithm> #i ...
- python globals和locals
文章里面说globals和locals函数返回的是命名空间 - 一个存有对应作用域的所有的变量.方法的字典,注意这里和dir函数返回数组的不一样 Python命名空间的本质 class Test(ob ...