Value tree is [0, 9], each path from the root to the leaf constitutes an integer, (the first number is the root), find all integers and all components of the
Depth-first search, by accumulating a integer parameter
class Solution { public: void helper(TreeNode* node, int path, int& sum){ if(!node){ return; } //a(node) //lk("root",node) //a(path) //dsp if(!node->left && !node->right){ sum+=path*10+node->val; //dsp return; } helper(node->left, path*10+node->val, sum); helper(node->right, path*10+node->val, sum); } int sumNumbers(TreeNode* root) { int sum=0; //ahd(root) //a(sum) helper(root, 0, sum); return sum; } };
Running dynamic presentations: http://simpledsp.com/FS/Html/lc129.html