3ms C++ recursive


  • 0
    X
    class Solution {
    public:
        int sumNumbers(TreeNode* root) {
            int result = 0;
            helper(root, 0, result);
            return result;
        }
        
        void helper(TreeNode* node, int path, int &result) {
            if (NULL == node) return;
            path += node->val;
            if (NULL == node->left && NULL == node->right) result += path;
            else {
                path *= 10;
                helper(node->left, path, result);
                helper(node->right, path, result);
            }
        }
    };
    

Log in to reply
 

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