199. Binary Tree Right Side View
来源:互联网 发布:wind万德数据库官网 编辑:程序博客网 时间:2024/05/20 21:21
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]
.
思路:层次遍历,遍历的时候用队列把右边的先放进去,把每层front的值放到结果中就可以了。
class Solution {public: vector<int> rightSideView(TreeNode* root) { vector<int> result;if(root == NULL) return result;queue<TreeNode*> layer;layer.push(root);result.push_back(root->val);while(!layer.empty()){int currentSize = layer.size();for(int i = 0; i < currentSize; i++){TreeNode* node = layer.front();layer.pop();if(node->right!=NULL) layer.push(node->right);if(node->left!=NULL) layer.push(node->left);}if(!layer.empty()) result.push_back(layer.front()->val);}return result; }};
0 0
- Tree-----199. Binary Tree Right Side View
- [LeetCode]199.Binary Tree Right Side View
- LeetCode 199. Binary Tree Right Side View
- [leetcode] 199.Binary Tree Right Side View
- [leetcode] 199. Binary Tree Right Side View
- [LeetCode]199. Binary Tree Right Side View
- 199. Binary Tree Right Side View LeetCode
- leetcode 199. Binary Tree Right Side View
- [LeetCode]199. Binary Tree Right Side View
- [LeetCode]199. Binary Tree Right Side View
- 199. Binary Tree Right Side View
- [LeetCode] 199. Binary Tree Right Side View
- LeetCode *** 199. Binary Tree Right Side View
- LeetCode 199. Binary Tree Right Side View
- 199. Binary Tree Right Side View
- 199. Binary Tree Right Side View
- 199. Binary Tree Right Side View
- 199. Binary Tree Right Side View
- 如何使用js动态创建table,并隔行换色
- butterknife
- 微信支付之扫码支付相关代码(Java)(转载)
- openCV 在ubuntu 虚拟机中的linux中安装,并且运行用控制台运行自行写helloworld
- 让apache开启.htaccess--让thinkphp框架的index.php在U地址不用写
- 199. Binary Tree Right Side View
- java容器类
- [160909][Java]二进制编辑器
- JSP入门1
- 亚马逊VPS node.js loopback 初次配置服务器端环境(Ubuntu环境)
- 人件读书笔记(01) 此时此刻,一个项目正在走向失败
- 最大搜索二叉树
- Runnable接口实现多线程
- 使用IDEA创建第一个Servlet工程