leetcode-Binary Tree Right Side View
2015-11-05 00:03
267 查看
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,
You should return
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].
/** * 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) { if(!root) return vector<int>(); deque<TreeNode*> que; vector<int> res; que.push_back(root); while(!que.empty()){ res.push_back(que.back()->val); int size=que.size(); while(size--){ TreeNode *temp=que.front(); que.pop_front(); if(temp->left) que.push_back(temp->left); if(temp->right) que.push_back(temp->right); } } return res; } };
相关文章推荐
- [2099]:整除尾数
- 两个select转移
- 【AngularJs】---Error: [$injector:modulerr] Failed to instantiate module starter.services
- 【CSS3】---块状元素、内联元素(又叫行内元素)和内联块状元素
- 【Ionic】---Using Local Notifications In Your Ionic Framework App
- 【CSS3】---元素隐藏(是否占据空间、是否可点击)
- 【Ionic】--- ngcordova插件
- 【AngularJs】---实现select的ng-options
- express全局安装了还是提示不是内部命令
- 【CSS3】---结构性伪类选择器-root+not+empty+target
- 【CSS3】---盒模型margin、padding及border
- 【NodeJS】-2-第一个node程序helloworld.js
- sublime text使用技巧
- 【NodeJS】-1-安装
- 如何改变链接的鼠标形状
- 页面自动跳转
- 【ERROR】---Error executing "adb devices":ADB server didn't ACK
- 【CSS3】---:before :after生成内容
- 【CSS3】---曲线阴影翘边阴影
- 【Ionic】---App名字和图标修改+启动画修改(SplashScreen)