LeetCode题解之Binary Tree Right Side View

时间:2023-11-11 08:36:02

1、题目描述

LeetCode题解之Binary Tree Right Side View

2、问题分析

使用层序遍历

3、代码

  vector<int> v;
vector<int> rightSideView(TreeNode* root) {
if (root == NULL)
return v; queue<TreeNode*> q;
q.push(root); while (!q.empty()) {
int size = q.size();
for(int i = ; i < size; i++) {
TreeNode *node = q.front();
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right); if (i == size - )
v.push_back(node->val);
q.pop();
}
} return v; }