题目描述
输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
/*
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> > FindPath(TreeNode* root,int expectNumber) {
if(root == NULL){
return vt;
}
num += root->val;
path.push_back(root->val);
if(num == expectNumber && root->left == NULL && root->right == NULL){
vt.push_back(path);
}
if(num < expectNumber && root->left!=NULL){
FindPath(root->left,expectNumber);
}
if(num < expectNumber && root->right!=NULL){
FindPath(root->right,expectNumber);
}
num -= root->val;
path.pop_back();
return vt;
}
private:
vector<vector<int>> vt;
vector<int> path;
int num = 0;
};