首页 > 其他 > 详细

【leetcode】Binary Tree Right Side View

时间:2015-05-22 11:16:19      阅读:76      评论:0      收藏:0      [点我收藏+]

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

 

 1 class Solution {
 2 public:
 3     vector<int> rightSideView(TreeNode* root) {
 4         vector<int> ret;
 5         if(root==NULL) return ret;
 6 
 7         queue<TreeNode*> Q;
 8         Q.push(root);
 9         TreeNode * node;
10         while(!Q.empty()){
11             int qlen=Q.size();
12             for(int i=0;i<qlen;i++){
13                 node=Q.front();
14                 Q.pop();
15 
16                 if(node->left) Q.push(node->left);
17                 if(node->right) Q.push(node->right);
18 
19             }
20             ret.push_back(node->val);
21         }
22 
23         return ret;
24     }
25 };

 

【leetcode】Binary Tree Right Side View

原文:http://www.cnblogs.com/jawiezhu/p/4521688.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!