Q:

Comparison odd behaviour

The following code never executes the if part... at the first call level is 0  and view.size() - 1 is -1,  but the else option is chosen instead ..
vector<int> rightSideView(TreeNode* root) {
        vector<int> solution;
        createView(root, solution, 0);
        return solution;

    }
    void createView(TreeNode* root, vector<int>& view, int level) {
        if(root == NULL)
            return;
        if(level > view.size() - 1) {
           view.push_back(root->val);
        }
        else {
            // only this part is executed
            view[level] = root->val;
        }
        createView(root->left, view, level + 1);
        createView(root->right, view, level + 1);

    }

The root is not NULL .. actually if I put a message in the else bracket and I have n nodes it will print n times. What's the reason for this odd behaviour?

A:

view.size() - 1 would be the largest number for std::size_t when view.size() is 0, as it is unsigned.
Use
if (level > static_cast<int>(view.size()) - 1) {

or
if (level + 1 > view.size()) {

