码迷,mamicode.com
首页 > 其他好文 > 详细

【leetcode】Binary Tree Right Side View

时间:2015-05-22 11:18:29      阅读:107      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!