Easy to understand C++ solution


  • 0
    M

    class Solution {

    vector<vector<int>> L;
    

    public:

    void traverse(TreeNode* root, int level)
    {
        if (root != NULL)
        {
            if (level==L.size())
                L.insert(L.end(),vector<int>(0));
    
            L.at(level).insert(L.at(level).end(),root->val);
            traverse(root->left,level+1);
            traverse(root->right,level+1);
        }
        else
            return;
    }
    
    vector<vector<int>> levelOrder(TreeNode* root) 
    {
        traverse(root,0);
        return L;
    }
    

    };


Log in to reply
 

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