112. 路径总和I、113. 路径总和 II、437. 路径总和 III

题目链接

1. 判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。

/**
 * 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 travel(TreeNode* root, int count) {
        if (!root->right && !root->left && count == 0) {
            return true;
        }
        if (!root->right && !root->left) 
            return false;

        if (root->left) {
            if(travel(root->left, count - root->left->val)) 
                return true;
        }
        if (root->right) {
            if (travel(root->right, count - root->right->val)) 
                return true;
        }
        return false;
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (!root) return false;
        return travel(root, targetSum - root->val);
    }
};

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:
    vector<int> path;
    vector<vector<int>> result;
    void travel(TreeNode* root, int count) {
        if (!root->right && !root->left && count == 0) {
            result.push_back(path);
            return ;
        }

        
        if (!root->right && !root->left) 
            return ;

        if (root->left) {
            path.push_back(root->left->val);
            travel(root->left, count - root->left->val);
            path.pop_back();
            
        }
        if (root->right) {
            path.push_back(root->right->val);
            travel(root->right, count - root->right->val);
            path.pop_back();
        }
        return ;
    }
    vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
        // result.clear();
        // path.clear();
        if (!root) return result;
        path.push_back(root->val);
        travel(root, targetSum - root->val);
        return result;
    }
};

3. 求该二叉树里节点值之和等于 targetSum 的 路径 的数目。

/**
 * 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 res;
    void travel(TreeNode* root, int count) {
        if (!root->right && !root->left && count == 0) {
            res++;
            return ;
        }

        if (!root->right && !root->left) 
            return ;

        if (root->left) {
            travel(root->left, count - root->left->val);
            
        }
        if (root->right) {
            travel(root->right, count - root->right->val);
        }
        return ;
    }    
    int pathSum(TreeNode* root, int targetSum) {
        res = 0;
        if (!root) return 0;
        travel(root, targetSum - root->val);
        return res;
    }
};

在这里插入图片描述

总结

  • 第三题,一直错的原因是没看清题目要求,路径 不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)
  • 明天再写吧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值