Binary] [path as a binary value

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/m0_38033475/article/details/91345248

The general idea:

Well binary tree, recursively. This question I use dfs to solve, find a good critical condition, pruning, pay attention to vector stored value is not a node.

One problem is, I do dfs vector array results obtained, not by descending to arrange the vector length, because it is possible dfs left subtree shorter route, but go first to the left subtree. Accordingly, it is necessary to sort the array vector and array subscripts record, to establish a new vector array.

I think the method is to map <int, int> represents "the vector subscript - the vector of length", and then on the "length" of sorts, key sorted adjusted index is a good order. But the need to implement vector, as follows.

map sort of : first written cmp function, then using vector <pair <int, int>to store the contents of the map, to sort vector. (See https://blog.csdn.net/m0_38033475/article/details/79309694 )

AC Code:

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    vector<vector<int> > result;
    
    void find(TreeNode* h,int expectNumber,int sum,vector<int> now) //dfs
    {
        now.push_back(h->val);
        sum += h->val;
        if(sum==expectNumber)
        {
            if(h->left==NULL && h->right==NULL) //必须要到达叶子结点
            {
                result.push_back(now);
                return;
            }
            else
                return;
        }
        if(sum>expectNumber) 
            return;
        //如果已经到了叶子结点但没达到目标值,则也退出
        if(h->left==NULL && h->right==NULL)
            return;
        if(h->left!=NULL)
            find(h->left,expectNumber,sum,now);
        if(h->right!=NULL)
            find(h->right,expectNumber,sum,now);
    }
    
    map<int, int>m; //下标-数组长度
    
    static bool cmp(pair<int,int> a, pair<int,int> b)
    {
        if(a.second>b.second)
            return true;
        else 
            return false;
    }
    
    
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) 
    {
        //先判=空或只有一个
        if(root==NULL)
            return result;
        if(root->left==NULL && root->right==NULL)
        {
            if(root->val==expectNumber)
            {
                vector<int> ans;
                ans.push_back(root->val);
                result.push_back(ans);
                return result;
            }
            else
                return result;
        }
        vector<int> ans;
        find(root,expectNumber,0,ans);
        
        //题目要求,在vector中数组长度大的靠前
        //先统计每个数组的长度
        for(int i=0;i<result.size();i++)
        {
            m[i]=result[i].size();
        }
        vector<pair<int,int> >v(m.begin(),m.end());
        sort(v.begin(),v.end(),cmp);
        vector<vector<int> > real_result;
        for(int i=0;i<result.size();i++)
        {
            real_result.push_back(result[v[i].first]);
        }
        return real_result;
    }
};

 

Guess you like

Origin blog.csdn.net/m0_38033475/article/details/91345248