C++ Solution beats 100% using inorder traversal


  • 0
    M

    '''
    class Solution {
    public:
    vector<vector<int>>level;
    void levelorder(TreeNode* root,int depth)
    {
    if(root==NULL)
    {
    return;
    }
    if(level.size()==depth)
    {
    level.push_back(vector<int>());
    }
    level[depth].push_back(root->val);
    levelorder(root->left,depth+1);
    levelorder(root->right,depth+1);
    }

    vector<int> rightSideView(TreeNode* root) {
        vector<int>res;
        levelorder(root,0);
        for(int i=0;i<level.size();i++)
        {
            res.push_back(level[i].back());
        }
        
        return res;
    }'''
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.