Created
May 21, 2024 05:50
-
-
Save shitu13/e5c76a040745fdab5de25630ec19e56a to your computer and use it in GitHub Desktop.
Binary Tree Right Side View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int> rightSideView(TreeNode* root) { | |
vector<int> res; | |
if (root == NULL) | |
return {}; | |
queue<TreeNode*> que; | |
que.push(root); | |
while (!que.empty()) { | |
int n = que.size(); | |
TreeNode* node = NULL; | |
while (n--) { | |
node = que.front(); | |
que.pop(); | |
if (node->left) | |
que.push(node->left); | |
if (node->right) | |
que.push(node->right); | |
} | |
res.push_back(node->val); | |
} | |
return res; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment