Leetcode 剑指Offer 34.二叉树中和为某一值的路径

class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int target) {
        vector<vector<int>> res;
        vector<int> path;
        findPath(root, target, res, path);
        return res;
    }
    void findPath(TreeNode* root, int target, vector<vector<int>>& res, vector<int>& path){
        if (root == nullptr) return;
        target -= root->val;
        path.push_back(root->val);
        if (!target && !root->left && !root->right) res.push_back(path);
        if (root->left) findPath(root->left, target, res, path);
        if (root->right) findPath(root->right, target, res, path);
        path.pop_back();
    }
};
上一篇:【每日一题】34. 图的遍历 (连通图,二分图)


下一篇:2021-05-24