leetcode 437. 路径总和 III

leetcode 437. 路径总和 III

题目链接

暴力双搜

复杂度 O(n^2)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    int dfs(TreeNode* root,int q)
        {
            if(!root)return 0;
            int res=0;
            if(root->val==q)res++;
            res+=dfs(root->left,q-root->val);
            res+=dfs(root->right,q-root->val);
            return res;
        }
    int pathSum(TreeNode* root, int q) {
        if(!root)return 0;
        int res=dfs(root,q);
        res+=pathSum(root->left,q);
        res+=pathSum(root->right,q);
        return res;
    }
};

哈希表+前缀和+dfs

复杂度O(n)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    unordered_map<int,int>hash;
    int res=0;
    int dfs(TreeNode* root,int q,int cur){
        if(!root)return 0;
        cur+=root->val;
        res+=hash[cur-q];
        hash[cur]++;  
        dfs(root->left,q,cur);
        dfs(root->right,q,cur);
        hash[cur]--;
        return res;      
    }
    int pathSum(TreeNode* root, int q) {
        if(!root)return 0;
        hash[0]=1;
        dfs(root,q,0);
        return res;
    }
};

leetcode 113. 路径总和 II

题目链接

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>>res;
    vector<int>path;
    vector<vector<int>> pathSum(TreeNode* root, int q) {
        if(!root)return res;
        dfs(root,q);
        return res;
    }
    void dfs(TreeNode* root,int q)
    {
        path.push_back(root->val);
        if(!root->left&&!root->right)
        {
            if(root->val==q)res.push_back(path);
            return ;
        }

        if(root->left){dfs(root->left,q-root->val);path.pop_back();}
        if(root->right){dfs(root->right,q-root->val);path.pop_back();}
    }
};
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_WAWA鱼_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值