力扣算法二叉树篇:路径总和

路径总和

在这里插入图片描述

递归:前序遍历

递归三部曲:
1、确定递归函数的参数和返回值
参数为目标树的根结点和目标值,返回值为bool类型,意为目标树是否存在一条从根结点到叶子结点的路径使得路径之和为目标值
再次:如果需要搜索整颗二叉树,那么递归函数就不要返回值,如果要搜索其中一条符合条件的路径,递归函数就需要返回值,因为遇到符合条件的路径了就要及时返回。
2、确定递归终止条件
遇到叶子结点即可返回停止,返回该条路径之和是否为目标值
3、确定单层递归逻辑
递归遍历,注意回溯和递归的返回值

/**
 * 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:
    bool pathSum(TreeNode* node,int count){
        //叶子结点
        if(node->left==nullptr && node->right==nullptr && count == 0){
            //存在路径
            return true;
        }
        //该路径之和不是目标值
        if(node->left == nullptr && node->right == nullptr){
            return false;
        }
        //单层处理
        if(node->left){
            count-=node->left->val;
            if(pathSum(node->left,count)){
                return true;
            }
            count += node->left->val;
        }
        if(node->right){
            count-=node->right->val;
            if(pathSum(node->right,count)){
                return true;
            }
            count += node->right->val;
        }
        return false;
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root == nullptr){
            return false;
        }
        return pathSum(root,targetSum-root->val);
    }
};

迭代:栈模拟前序遍历

栈中的元素应该是结点和该结点对应的路径值(从根结点到该结点)

class Solution {
public:
  
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root == nullptr){
            return false;
        }
        //栈模拟前序遍历
        stack<pair<TreeNode*,int>> st;
        st.push(pair<TreeNode*,int>(root,root->val));
        //遍历
        while(!st.empty()){
            pair<TreeNode*,int> node = st.top();
            st.pop();
            //判断是否为叶子结点且count为目标值
            if(node.first->left == NULL && node.first->right == NULL && node.second == targetSum){
                return true;
            }
            //右节点
            if(node.first->right){
                st.push(pair<TreeNode*,int>(node.first->right,node.second+node.first->right->val));
            }
            //左节点
            if(node.first->left){
                st.push(pair<TreeNode*,int>(node.first->left,node.second+node.first->left->val));
            }
        }
        return false;
    }
};

路径总和II

在这里插入图片描述
递归
递归三部曲:
1、确定递归函数的参数和返回值
参数为目标树的根结点和目标值,无返回值
2、确定递归终止条件
遇到叶子结点即可停止,记录符合要求的路径
3、确定单层递归逻辑
递归遍历,注意回溯

/**
 * 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>> result;
    vector<int> path;
    void traversal(TreeNode* node,int count){
        //叶子结点 符号所求
        if(node->left == NULL && node->right == NULL && count == 0){
            //记录路径
            result.push_back(path);
            return;
        }
        //叶子结点 但路径不符合所求
        if(node->left == NULL && node->right == NULL){
            return;
        }
        //递归遍历
        if(node->left){
            count-=node->left->val;
            path.push_back(node->left->val);
            //递归
            traversal(node->left,count);
            //回溯
            count+=node->left->val;
            path.pop_back();
        }
        //递归遍历
        if(node->right){
            count-=node->right->val;
            path.push_back(node->right->val);
            //递归
            traversal(node->right,count);
            //回溯
            count+=node->right->val;
            path.pop_back();
        }
        return ;
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        result.clear();
        path.clear();
        if(root == NULL){
            return result;
        }
        path.push_back(root->val);
        traversal(root,targetSum-root->val);
        return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值