Leetcode-113: Path Sum II

解法1:回溯

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

void helper(TreeNode* node, int sum, vector<int>& v, vector<vector<int> >&vv) {
     if (!node) return;
     if ((node->val==sum) && (!node->left) && (!node->right)) { //remember it is from the root to leaf
        v.push_back(node->val);
        vv.push_back(v);
        v.pop_back();
        return;
     }

     v.push_back(node->val);
     helper(node->left, sum-node->val, v, vv);
     helper(node->right, sum-node->val, v, vv);
     v.pop_back();

}

vector<vector<int>> pathSum(TreeNode* root, int sum) {
    vector<vector<int>> vv;
    if (!root) return vv;
    vector<int> v;
    helper(root, sum, v, vv);
    return vv;
}

int main()
{
    TreeNode a(5);
    TreeNode b(4);
    TreeNode c(8);
    TreeNode d(11);
    TreeNode e(13);
    TreeNode f(4);
    TreeNode g(7);
    TreeNode h(2);
    TreeNode i(5);
    TreeNode j(1);

    a.left=&b;
    a.right=&c;
    b.left=&d;
    c.left=&e;
    c.right=&f;
    d.left=&g;
    d.right=&h;
    f.left=&i;
    f.right=&j;

    vector<vector<int>> vv;
    vv=pathSum(&a, 22);

    cout<<"["<<endl;
    for (auto v : vv) {
        cout<<"  [ ";
        for (auto i : v) {
            cout <<i<<" ";
        }
        cout<<"],"<<endl;
    }
    cout<<"]"<<endl;
    return 0;
}

二刷:

class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        target = targetSum;
        vector<int> sol;
        helper(root, sol);
        return sols;
    }
private:
    vector<vector<int>> sols;
    int sum = 0, target = 0;
    void helper(TreeNode *root, vector<int> &sol) {
        if (!root) return;
        sum += root->val;
        sol.push_back(root->val);
        if (!root->left && !root->right && sum == target) {
            sols.push_back(sol);
        }
        helper(root->left, sol);
        helper(root->right, sol);
        sum -= root->val;
        sol.pop_back();
    }
};

不用引用也可以

class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        int sum = 0;
        vector<int> sol;
        target = targetSum;
        helper(root, sol, sum);
        return sols;
    }
private:
    vector<vector<int>> sols;
    int target = 0;
    void helper(TreeNode *root, vector<int> sol, int sum) {
        if (!root) return;
        sum += root->val;
        sol.push_back(root->val);
        if (!root->left && !root->right && sum == target) {
            sols.push_back(sol);
        }
        helper(root->left, sol, sum);
        helper(root->right, sol, sum);
        //sum -= root->val;
        //sol.pop_back();
    }
};

解法2:分治

class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        int sum = 0;
        vector<int> sol;
        target = targetSum;
        return helper(root, sol, sum);
    }
private:
    int target = 0;
    vector<vector<int>> helper(TreeNode *root, vector<int> sol, int sum) {
        if (!root) return {};
        vector<vector<int>> res;
        sol.push_back(root->val);
        sum += root->val;
        if (!root->left && !root->right && sum == target) {
            res.push_back(sol);
        }
        vector<vector<int>> res_left = helper(root->left, sol, sum);
        vector<vector<int>> res_right = helper(root->right, sol, sum);
        res.insert(res.end(), res_left.begin(), res_left.end());
        res.insert(res.end(), res_right.begin(), res_right.end());
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值