class Solution {
public:
void recursion(TreeNode *root, int level, vector<int> &res)
{
if(root==NULL) return ;
if(res.size()<level) res.push_back(root>val);
recursion(root>right, level+1, res);
recursion(root>left, level+1, res);
}
vector<int> rightSideView(TreeNode *root) {
vector<int> res;
recursion(root, 1, res);
return res;
}
};
My C++ solution, modified preorder traversal



simple level traversal
class Solution { public: vector<int> rightSideView(TreeNode* root) { if(!root) return vector<int>(); queue<TreeNode*> inque,outque; outque.push(root); queue<TreeNode*>& ref_outque = outque;//optimize it without memory copy vector<int> res; while(!ref_outque.empty()) { TreeNode* tem = ref_outque.front(); ref_outque.pop(); if(tem>left) inque.push(tem>left); if(tem>right) inque.push(tem>right); if(ref_outque.empty()) { res.push_back(tem>val); //right side ref_outque = inque; inque = queue<TreeNode*>(); } } return res; } };

@DaGgER14 Yes, I get why he has done that; but I didn't fully understand how checking the size of the vector denotes the level. Could you please elaborate with some examples?

@gavin5 But at the time submission it gives error void declare the recursion .. please help