My 4ms c++ solusion


  • 0
    C
    class Solution {
    public:
        vector<vector<int>> levelOrderBottom(TreeNode* root) {
            vector<vector<int>> v;
            traversal(v, root, 0);
            reverse(v.begin(),v.end());
            return v;
        }
        
        void traversal(vector<vector<int>> & v, TreeNode* node, int depth){
            if(node){
                traversal(v, node->left, depth+1);
                traversal(v, node->right, depth+1);
                if(depth + 1 >= v.size()){
                    v.resize(depth + 1);
                };
                v[depth].push_back(node->val);
            }
        }
    };

Log in to reply
 

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