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

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

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

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

  3. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  4. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  5. 【LeetCode】Balanced Binary Tree(平衡二叉树)

    这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...

  6. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  7. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

  8. 【LeetCode】Balanced Binary Tree 算法优化 解题报告

    Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...

  9. 【LeetCode】145. Binary Tree Postorder Traversal 解题报告 (C++&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

随机推荐

  1. Ios之网络编程NSURLConnection

    通过NSURLConnection主要通过四个类进行网络访问:NSURL,NSURLRequest,NSMutableURLRequest,NSURLConnection 一.基本知识 (1)NSUR ...

  2. 【python3】集合set (转)

    https://www.cnblogs.com/onepeace/p/4791578.html set原理 Python 还 包 含 了 一 个 数 据 类 型—— set ( 集 合 ) . 集 合 ...

  3. __attribute__中constructor和destructor[总结]

    1.前言 最近看到一份代码,看到一个函数前面用__attribute__((destructor))修饰,当时感觉有点怪怪的,搜了整个程序,也没发现哪个地方调用这个函数.于是从字面意思猜想,该函数会在 ...

  4. 关于ZedGraph几个难点

    1.引言 由于工作原因,需要对数据进行图表展示,.NET默认的控件库或者DotNetBar的控件库又不能满足需求.去网上找到了这个开源的ZedGraph绘制2D图表的程序集,之所以选择这个ZedGra ...

  5. OpenStack云计算之路-Mitaka 版本

    1.1 云计算简介 云计算(英语:cloud computing ),是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需求提供给计算机各种终端和其他设备. 云计算是继1980年代 ...

  6. SpringBoot添加对jsp的支持

    1.在pom.xml添加如下内容: <dependency> <groupId>org.apache.tomcat.embed</groupId> <arti ...

  7. 013-Go通archive/zip生成ZIP文件

    package main import( "io/ioutil" "os" "bytes" "archive/zip" ...

  8. Tomcat下HTTPS双向认证配置以及客户端调用案例

    1:生成服务器端的keystore和truststore文件 (1)以jks格式生成服务器端包含Public key和Private Key的keystore文件 keytool -genkey -a ...

  9. sell 项目 商品表 设计 及 创建

    1.数据库表之间的关系说明 2.数据库设计 3.创建 商品表 /** * 商品表 */ create table `product_info` ( `product_id` varchar(32) n ...

  10. ViewPage+frament不预载入下一个Frament数据解决的方法

    在做一个ViewPage+Frament 滑动数效果,当滑动到每一页时载入哪一页的数据,可是ViewPage会预载入下一也数据.这个问题之前做项目是一直未解决,今天找到一个方法一下子就解决的这个问题, ...