clean c++ solution using traversal function


  • 0

    class Solution {
    public:
    int sumNumbers(TreeNode* root) {
    vector<int>temp;
    vector<vector<int>>final;
    int result=0;

        traversal(final,root,temp);
        for(int i=0;i<final.size();++i){
            int multi=1;
            for(int j=final[i].size()-1;j>=0;--j){
                result=final[i][j]*multi+result;
                multi*=10;
            }
        }
            
        return result;
    }
    void  traversal(vector<vector<int>>&vT,TreeNode*root,vector<int>n){
        if(root){
            n.push_back(root->val);
            traversal(vT,root->left,n);
            travesral(vT,root->right,n);
            if(!root->left&&!root->right)
            vT.push_back(n);
        }        
    }
    

    };


Log in to reply
 

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