Leetcode 199 Binary Tree Right Side View 二叉树右视图
2015-10-29 10:53
330 查看
原题地址
https://leetcode.com/problems/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 <---[/code]
You should return [1, 3, 4].
你需要返回[1, 3, 4].
解题思路
首先可以明确的是,看到的结点是每一层的结点中最右侧的结点,我们要做的就是按照层序把每一层最右侧的结点找出来并返回.然后针对按照层序的关键点,我们可以考虑层序遍历的类似思路.层序遍历中需要用到队列,这里我们考虑也使用队列.但是与层序遍历不同的是,我们需要非常明确的区分开不同的层,以保存每层的最后一个结点.我们可以使用两个队列,一个队列中存储某一层的结点,另一个队列存储下一层的结点,这样做到把不同层的结点区分开来.原题地址
https://leetcode.com/problems/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 <---[/code]
You should return [1, 3, 4].
你需要返回[1, 3, 4].
解题思路
首先可以明确的是,看到的结点是每一层的结点中最右侧的结点,我们要做的就是按照层序把每一层最右侧的结点找出来并返回.然后针对按照层序的关键点,我们可以考虑层序遍历的类似思路.层序遍历中需要用到队列,这里我们考虑也使用队列.但是与层序遍历不同的是,我们需要非常明确的区分开不同的层,以保存每层的最后一个结点.我们可以使用两个队列,一个队列中存储某一层的结点,另一个队列存储下一层的结点,这样做到把不同层的结点区分开来.代码 cpp
class Solution { public: /** 获取每一层最右侧的结点 */ vector<int> rightSideView(TreeNode* root) { /* 两个队列交替使用,获取队尾的元素 */ vector<int> ret; queue<TreeNode*> queues[2]; /* 初始化,把第一层结点放入第一个队列*/ if (root != NULL) queues[0].push(root); /* 交替使用队列,一个队列存储一层结点 * 另一个队列存储下一层结点(或上一层结点) */ int i = 0, j = 1, tmp; TreeNode *p; while (!queues[0].empty() || !queues[1].empty()) { while (!queues[i].empty()) { p = queues[i].front(); queues[i].pop(); if (p->left) queues[j].push(p->left); if (p->right) queues[j].push(p->right); tmp = p->val; } // 遍历完某一层结点时,tmp是该层最右侧的结点的值 ret.push_back(tmp); // 添加到结果集 i = (i + 1) % 2; // 交换两个队列的角色 j = (j + 1) % 2; } return ret; } };
相关文章推荐
- 元器件小积累
- django.forms-Widget和Media间的联系
- 键盘遮挡
- Server2008R2:由于没有远程桌面授权服务器可以提供许可证,.....错误的解决
- Struts MVC与Spring MVC的区别
- Leetcode -- The Skyline Problem
- 肠道好坏影响寿命
- inner join中的where和on
- maven构建项目时,出现aspectj-maven-plugin 无法构建的问题
- 解读:在命令情况下使用Android NDK交叉编译工具
- 内存溢出与内存泄露的区别
- dajngo.forms.widget-Media
- 存储过程加密解密
- 常见的Regex表达式(更新RFC标准的email检验)
- Activity设置全屏的三种方法
- AccessRandomFile多线程下载文件
- 引用&
- 我做运营那些年,如何找到用户并留住用户?
- QT5入门之24 -QT 容器
- Git学习笔记