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 };
Mamba never out

浙公网安备 33010602011771号