Sum Root to Leaf Numbers

class Solution {

public:

int sumNumbers(TreeNode* root) {

int sum=0;// sum is the path of each route. It is deepth search

return helper(root,sum);

}

int helper(TreeNode* root, int sum){

if(!root) return 0;

if(!root->left && !root->right)

return sum*10+root->val;

return helper(root->left,sum10+root->val)+helper(root->right,sum10+root->val);

}

};

Last updated

Was this helpful?