递归

class Solution {
public:
    vector<vector<int>> ans;
    vector<int> path;
    void backtra(TreeNode* root,int expectNumber){
        if(!root->left&&!root->right){
            if(root->val==expectNumber){
                path.push_back(root->val);
                ans.push_back(path);
                path.pop_back();
            }else return;
        } 
        path.push_back(root->val);
        if(root->left) backtra(root->left, expectNumber-root->val);
        if(root->right) backtra(root->right,expectNumber-root->val);
        path.pop_back();
    }
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        if(!root) return ans;
        backtra(root,expectNumber);
        return ans;
    }
};

看评论有的说输出结果要排序,题目中好像说的没有啊,也许是题目又变了?