We need to judge all the paths from the root node to the leaf node, and multiply the nodes on each path from the root node to the leaf node for 10 operations, similar to the conversion of strings to int operations, specific problems see: https://www.nowcoder.com/practice/ 185a87cd29eb42049132aed873273e83?tpid=46&tqid=29051&rp=1&ru=/ta/leetcode&qru=/ta/leetcode/ Question-ranking, the following is solved:
/** * Definition for binary tree * struct TreeNode {* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode (int x): Val (x), left (null), right (NULL) {} *};
* * Class Solution {public:int sumnumbers (TreeNode *root) {int allsum=0;
Vector<int> VEC;
Pathvec (Root,vec);
Vector<vector<int>>::iterator Iter=allpaths.begin ();
while (Iter!=allpaths.end ()) {allsum+=count (*iter);
iter++;
return allsum;
int count (vector<int> vec) {vector<int>::iterator iter=vec.begin ();
int sum=0;
while (Iter!=vec.end ()) {sum=sum*10+*iter;
iter++;
return sum;
} void Pathvec (treenode* root,vector<int>& onepath) {//vector<vector<int>> allpaths;
if (root!=null) onepath.push_back (root->val);
else return; if (root->left==null&&root->right==null) Allpaths.push_back (OnePath);
if (root->left) Pathvec (Root->left,onepath);
if (root->right) Pathvec (Root->right,onepath);
Onepath.pop_back ();
} private:vector<vector<int>> allpaths; };
It's important to remember that after each addition, you need to add the last POPs, that is, the onepath.pop_back () action, by adding each path to the allpaths or the binary tree from the root node to the leaf node of all paths.
There is also a need to solve the problem is how not through the private structure of the allpaths structure, but in the process of recursion to set the allpaths, that is, c thinking of the solution ...
A new idea emerged ...
The storage can be updated by setting the All_path parameter at recursion time.
Class Solution {public:int sumnumbers (TreeNode *root) {int allsum=0;
Vector<int> VEC;
Vector<vector<int> >all_path;
Pathvec (Root,vec,all_path);
Vector<vector<int>>::iterator Iter=all_path.begin ();
while (Iter!=all_path.end ()) {allsum+=count (*iter);
iter++;
return allsum;
int count (vector<int> vec) {vector<int>::iterator iter=vec.begin ();
int sum=0;
while (Iter!=vec.end ()) {sum=sum*10+*iter;
iter++;
return sum; } void Pathvec (treenode* root,vector<int> v,vector<vector<int> >& vv) {if (root==null) re
Turn
V.push_back (Root->val);
if (root->left==null&&root->right==null) Vv.push_back (v);
if (root->left) Pathvec (ROOT->LEFT,V,VV);
if (root->right) Pathvec (ROOT->RIGHT,V,VV); V.pop_bACK (); }
};