class Solution {
public:
queue<TreeNode*> q;
vector<int> ans;
vector<int> rightSideView(TreeNode* root) {
if(!root)
return ans;
q.push(root);
bfs();
return ans;
}
void bfs(){
while(!q.empty()){
int sz = q.size();
TreeNode *temp = q.front();
ans.push_back(q.back()->val);
for(int i = 0; i < sz; ++i){
TreeNode *temp = q.front();
q.pop();
if(temp->left) q.push(temp->left);
if(temp->right) q.push(temp->right);
}
}
}
};
199. Binary Tree Right Side View
最新推荐文章于 2024-01-19 11:16:08 发布