Enter the root node and an integer of a binary tree, and print out all paths where the sum of the node values in the binary tree is the input integer. Pop explanation in.

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
#include<vector>
#include<algorithm>
class Solution {
public:
    vector<vector<int>>res;
    vector<int>buffer;
    static bool cmp( const vector<int> &a, const vector<int> &b)
    {
	return a.size() > b.size();
    }
    vector<vector<int> > FindPath1(TreeNode* root,int expectNumber)
    {
        if(root == NULL)return res;
        buffer.push_back(root->val);
        if (expectNumber-root-> val == 0 && root-> left == NULL && root-> right == NULL) 
	    sort (res.begin (), res.end (), cmp);
        { 
            res.push_back (buffer); 
        } 
        if (root-> left! = NULL) FindPath (root-> left, expectNumber-root-> val); 
        if (root-> right! = NULL) FindPath (root-> right , expectNumber-root-> val); 
        if (buffer.size ()! = 0) 
        buffer.pop_back (); // Every time you press in, it pops up as much as you want. 
        // if (res.size ()! = 0) 
        return res; 
    } 
    vector <vector <int>> FindPath (TreeNode * root, int expectNumber) 
    { 
        FindPath1 (root, expectNumber); // This sub-function is for filtering Come out of these paths 
};

 

 

Guess you like

Origin www.cnblogs.com/littleswan/p/12706430.html