//输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
bool TreeTraversePre(TreeNode* root,vector<vector<int> > &vec_Result, vector<int> &vec_Temp, int expectNumber)
{
if(NULL == root) return false;
vec_Temp.push_back(root->val);
if(expectNumber - root->val > 0)
{
expectNumber -= root->val;
if(NULL != root->left)
{
TreeTraversePre(root->left, vec_Result, vec_Temp, expectNumber);
vec_Temp.pop_back();
}
if(NULL != root->right)
{
TreeTraversePre(root->right, vec_Result, vec_Temp, expectNumber);
vec_Temp.pop_back();
}
}else if(expectNumber - root->val < 0)
{
return false;
}else if(NULL == root->left && NULL == root->right)
{
vec_Result.push_back(vec_Temp);
return true;
}
return false;
}
vector<vector<int> > FindPath(TreeNode* root,int expectNumber)
{
vector<vector<int> > vecResult;
vector<int> vecTemp;
TreeTraversePre(root, vecResult, vecTemp, expectNumber);
return vecResult;
}