剑指offer 二叉树中和为某一值得路径

class Solution {
public:
    void recur(TreeNode* root, const int expectNumber, int curr, vector<vector<int>>& res, vector<int>& path){
        path.push_back(root->val);
        curr += root->val;
        bool isLeaf = (root->left == nullptr && root->right == nullptr);
        if(isLeaf && curr == expectNumber){
            res.push_back(path);
        }
        if(root->left != nullptr){
            recur(root->left, expectNumber, curr, res, path);
        }
        if(root->right != nullptr){
            recur(root->right, expectNumber, curr, res, path);
        }
        path.pop_back();
    }

    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        vector<vector<int>> res;
        vector<int> path;
        if(root == nullptr) return res;
        int curr = 0;
        recur(root, expectNumber, curr, res, path);
        return res;
    }
};

posted @ 2018-09-02 20:27  一条图图犬  阅读(93)  评论(0编辑  收藏  举报