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].

这是一道medium的题,但是刚开始感觉不难。首先看到这道题的第一反应就是层序遍历,用队列。以前考研的时候看到过层序遍历,不过真心不记得了。后来自己想偏了,又说想用栈。

这道题就是把每一层的节点从左到右的入队(此时就可以计算一下队的大小,这个我一直没有想到,这样就可以计算出每一层有多少个节点了。),然后取最后一个节点的值入结果vector。假设我们第一层扫描完了(即它的孩子已经入队了),每扫描完了以后就会删掉你扫描的节点,那么当你把第一层的节点扫描完,第二层节点已经入队且第一层的节点已经被删除掉了,此时队列里只有第二层的节点,然后我们取最后一个的值入结果vector,就是最右的(因为我们是从左到右入队的)。同理,扫描第二层节点,扫描完了以后第二层节点已经全部被删除,队列里只有第三层节点。直至队列里没有节点了,说明新的一层一个节点没有,那就是树到底了。附代码。(这道题自己没做出来,后来参考别人的。。

using namespace std;
/**
* 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) {
int val=,sizequeue=;
vector<int> result;
if(root==NULL) return result;
queue<TreeNode> TreeQueue;
TreeQueue.push(*root);
while(!TreeQueue.empty()){ val=(TreeQueue.back()).val;
result.push_back(val);
sizequeue=TreeQueue.size();
for(int i=;i<sizequeue;i++){ *root=TreeQueue.front();
TreeQueue.pop(); if(root->left!=NULL) TreeQueue.push(*(root->left));
if(root->right!=NULL) TreeQueue.push(*(root->right)); } }
return result;
}
};

leetcode Binary Tree Right Side的更多相关文章

  1. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  2. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  3. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  4. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  5. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  6. [LeetCode] Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  7. [LeetCode] Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  8. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  9. [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  10. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

随机推荐

  1. 基于REM的移动端响应式适配方案

    视口 在前一段时间,我曾经写过一篇关于viewport的文章.最近由于在接触移动端开发,对viewport有了新的理解.于是,打算重新写一篇文章,介绍移动端视口的相关概念. 关于这篇文章说到的所有知识 ...

  2. javascript闭包的妙用——实现函数的重载

    最近在看John Resig 与 Bear Bibeault的<JavaScript 忍者秘籍>.这本书处处提现了js的魔法(从我这个写强类型语言的人看来).js能够点石成金,处处体现着它 ...

  3. Socket异步通信及心跳包同时响应逻辑分析。

    有段时间没有更博了,刚好最近在做Socket通信的项目,原理大致内容:[二维码-(加logo)]-->提供主机地址和端口号信息(直接使用[ThoughtWorks.QRCode.dll]比较简单 ...

  4. mysql sql 基础总结

    1 mysql top n使用 select * from table limit n; 2    统配符使用必须和like结合使用 like % 通配符 描述 % 替代一个或多个字符 _ 仅替代一个 ...

  5. C# TryParse()用法

    形式(以decimal为例): decimal.TryParse(str1,out num1) 功能:将str1转化成decimal类型,若转化成功,将值赋给num1,并返回true; 若转化失败,返 ...

  6. Oracle PIVOT 行转列方法

    数据库中業種的存储如下图: SELECT * FROM M_TORIHIKISAKI_GYOSYU 其中GYIUSYU_CD字段代表不同的業種 而画面需要实现下图所示样式:(将每条数据的業種横向展开显 ...

  7. kali ssh服务连接问题,无法远程管理

    1.修改sshd_config文件,命令为:vi /etc/ssh/sshd_config 2.将#PasswordAuthentication no的注释去掉,并且将NO修改为YES 3.将#Per ...

  8. SpringMVC原理及非注解配置详解

    1. Spring介绍 Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单. 这些控制器 ...

  9. 介绍CSS的相关知识

    以下是我跟大家分享的有关CSS的相关知识点: ①什么是CSS? css(Cascading Style Sheets)是层叠样式表 ②css的三种样式使用方法: 1,内联样式表:直接在html标签属性 ...

  10. 大数据与Java的关系

    随着2017年大数据各种应用的发展,大数据的价值得以充分的发挥,大数据已在企业.社会各个层面都成为重要的手段,数据已成为新的企业战略制高点,也是各个企业争夺的新焦点.那么我们一直在说着的大数据究竟是什 ...