recursive faster c++ simple solution


  • 0
    D
    class Solution {
        vector<vector<int>> ret;
    public:
        vector<vector<int>> levelOrder(TreeNode* root) 
        {
            solve(root, 0);
            return ret;
        }
        void solve(TreeNode* root, int level)
        {
            if(root == NULL) return;
            if(ret.size() < level + 1) ret.resize(level + 1);
            ret[level].push_back(root->val);
            solve(root->left, level + 1);
            solve(root->right, level + 1);
        }
    };

Log in to reply
 

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