leetcode-113-Path Sum II

问题

题目:[leetcode-113I]

思路

和之前的那一道只是在接口上有区别而已。

代码

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

            if(!root->left && !root->right){
                if(res==sum) ret.push_back(nodes);
            }
            dfs( root->left, sum, res, nodes, ret );
            dfs( root->right, sum, res, nodes, ret );
        }
    }
};

思路1

保持了和path sum一样的做法,在传递参数的时候,传递了path的引用,那么当前节点访问完毕之后,要把该节点退出path。

代码1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        std::vector<std::vector<int>> ret;
        if(!root) return ret;
        std::vector<int> path;
        dfs(root, sum, path, ret);

        return ret;
    }
private:
    void dfs(TreeNode* root, int sum, std::vector<int>& path, std::vector<std::vector<int>>& ret){
        if(!root) return;
        path.push_back(root->val);

        if(!root->left&&!root->right&&root->val==sum) ret.push_back(path);
        dfs(root->left, sum-root->val, path, ret);
        dfs(root->right, sum-root->val, path, ret);
        path.pop_back();
    }
};

思路2

二叉树的题目,对边界情况的处理比较灵活,其实就那么几种情况,如果不确定,分别试下即可。这个题dfs遍历即可。由于path是引用,所以头尾插入和删除是对称的,这样不会影响其他层次的遍历。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int sum) {
        vector<vector<int>> ret;
        if(!root) return ret;

        vector<int> path;

        dfs( root, 0, sum, path, ret );
        return ret;
    }
private:
    void dfs(TreeNode* root, int cur, int target, vector<int>& path, vector<vector<int>>& ret){
        if( root ){

            path.push_back(root->val);
            cur += root->val; // cur本身就是值传递,当前层变了之后也不会影响上一层

            if( !root->left && !root->right ){
                if( cur == target )
                    ret.push_back(path);
            }else{
                dfs(root->left, cur, target, path, ret);
                dfs(root->right, cur, target, path, ret);
            }
            path.pop_back();

        }
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值