A BFS solution that preserves all paths of a tree root to the sum of the leaves for a given value

Source: Internet
Author: User

BFS is a more intuitive solution. The disadvantage is to borrow a lot of data structure to help, perhaps can find ways to avoid.

When you need the path of the tree, it tends to re-engage a data structure, saving the parent-to-child backtracking chain, which is easy to implement.

But it does waste time and space, and one of the ways to avoid it is redundant storage. All ancestor node information is stored sequentially in each node.

Thus, when the node is selected, its ancestors are naturally determined. The number of the subject can be separated by a delimiter, such as "#", to complete the construction of the final answer.

/** * Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * Tre Enode (int x): Val (x), left (null), right (NULL) {} *}; */class Solution {public:vector<vector<int> > Pathsum (TreeNode *root, int sum) {VECTOR&LT;VECTOR&L        t;int>> Res;        Vector<int> path;                TreeNode *tmp;                if (!root) return res;        Queue<pair<treenode *,int>> Q;        Unordered_map<treenode*,treenode *> Parent;        Parent[root]=nullptr;                Q.push (Make_pair (root,root->val));            while (!q.empty ()) {Auto Tmp=q.front (); Q.pop ();            Auto node = Tmp.first;                        Auto num = Tmp.second;                        if (!node->left&&!node->right) {if (num==sum) {while (node!=nullptr) {                        Path.push_back (Node->val);                    Node=parent[node];        }            Reverse (Path.begin (), Path.end ());                    Res.push_back (path);                Path.clear ();            } continue;                } if (Node->left) {Q.push (Make_pair (node->left,num+node->left->val));            parent[node->left]=node;                } if (node->right) {Q.push (Make_pair (node->right,num+node->right->val));            parent[node->right]=node;            }} return res; }};


A BFS solution that preserves all paths of a tree root to the sum of the leaves for a given value

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.