A simple and fast solution in C++

  • 0

    On the submission page, it shows 0ms. Faster then other C++ solutions.
    The idea of solution is not new, this is just a simple way to implement it.

       class Solution {
        int result= 0;
        int sumNumbers(TreeNode* root) {
            if(root) pathSum(root,root->val);
            return result;
        void pathSum(TreeNode * root, int pathResult)
            if(!root->left && !root->right)
                result = result+pathResult;
            if(root->left ) 
                pathSum(root->left, pathResult*10 + root->left->val);
                pathSum(root->right, pathResult*10 + root->right->val);

Log in to reply

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