199. 二叉树的右视图

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution 
11 {
12 public:
13     vector<int> rightSideView(TreeNode* root) 
14     {
15         vector<int> res;
16         if(!root) return res;
17 
18         queue<TreeNode*> q;
19         q.push(root);
20         while(!q.empty())
21         {
22             int n = q.size();
23             int temp = 0;
24             for(int i = 0;i < n;i ++)
25             {
26                 auto a = q.front();
27                 q.pop();
28                 if(a->left) q.push(a->left);
29                 if(a->right) q.push(a->right);
30                 temp = a->val;
31             }
32             res.push_back(temp);
33         }
34         return res;
35     }
36 };

 

posted @ 2020-03-30 16:13  Jinxiaobo0509  阅读(125)  评论(0)    收藏  举报