June 24, 2022 · 1 min read
https://leetcode.com/problems/binary-tree-right-side-view/
class Solution {
vector<int> v;
void rightViewUtil(TreeNode *root, int lvl) {
if (root == nullptr)
return;
if (v.size() <= lvl)
v.push_back(root->val);
rightViewUtil(root->right, lvl + 1);
rightViewUtil(root->left, lvl + 1);
}
public:
vector<int> rightSideView(TreeNode *root) {
rightViewUtil(root, 0);
return v;
}
};