99、【树与二叉树】leetcode ——113. 路径总和 II:回溯法两种版本(C++版本)

题目描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
原题链接:113. 路径总和 II

解题思路

设置一个path存当前路径,设置一个res存结果集。

一、当前遍历,添加当前元素

此方式是,每次遍历时候,将当前结果添加到path当中,若找到和为targetSum的,则输出,否则分别再向左子树、右子树探测。函数结尾时候,再把添加的这个结点pop

/**
 * 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;
    void traversal(TreeNode* root, int targetSum, vector<int> path) {            
        // 遍历到一次加入加入该节点    
        path.push_back(root->val);
        targetSum -= root->val;
        if(!root->left && !root->right) {
            if(targetSum == 0) {
                res.push_back(path);                      
                return ;  
            }            
        }        
        if(root->left) {            
            traversal(root->left, targetSum, path);            
        }
        if(root->right) {
            traversal(root->right, targetSum, path);            
        }        
        // 注意pop当前节点要放到最后,而不是左一次右一次,否则会出现重复弹出
        path.pop_back();
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if(!root)       return {};
        vector<int> path;
        traversal(root, targetSum, path);
        return res;
    }
};

二、当前遍历,添加未来元素

此方式是,每次判定出左子树或右子树为空时,就提前将该结果加入path当中,退出时候,再分别从退出口pop先加入的元素。

/**
 * 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;
    void traversal(TreeNode* root, int targetSum, vector<int> path) {            
        if(!root->left && !root->right) {
            if(targetSum == 0) {
                res.push_back(path);                      
                return ;  
            }            
        }        
        if(root->left) {            
            path.push_back(root->left->val);    // 遍历此结点
            traversal(root->left, targetSum - root->left->val, path);            
            path.pop_back();    // 弹出加入结点
        }
        if(root->right) {
            path.push_back(root->right->val);
            traversal(root->right, targetSum - root->right->val, path);            
            path.pop_back();
        }        
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        if(!root)       return {};
        vector<int> path;
        path.push_back(root->val);
        traversal(root, targetSum - root->val, path);
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

辰阳星宇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值