C + + solution all paths from the root node to the leaf node in the binary tree---"Those strange Algorithms" __web

Source: Internet
Author: User

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-&GT;LEFT,V,VV);
        if (root->right) Pathvec (ROOT-&GT;RIGHT,V,VV); V.pop_bACK (); }
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.