112. Path Sum

Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.

A leaf is a node with no children.

Example 1:

Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.

Example 2:

Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.

Example 3:

Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.

Constraints:

  • The number of nodes in the tree is in the range [0, 5000].
  • -1000 <= Node.val <= 1000
  • -1000 <= targetSum <= 1000

法一:深度优先搜索

class Solution {
private:
    bool traversal(TreeNode*root,int count){
        if(root->left==NULL && root->right==NULL && count==0)return true;
        if(root->left==NULL && root->right==NULL)return false;

        if(root->left){
            count-=root->left->val;
            if(traversal(root->left,count))return true;
            count+=root->left->val;
        }
        if(root->right){
            count-=root->right->val;
            if(traversal(root->right,count))return true;
            count+=root->right->val;
        }
        return false;
    }
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root==NULL)return false;
        return traversal(root,targetSum-root->val);
    }
};

注意:

1,前提条件:

1)binary Tree

2)求一条路径的targetSum

2,解题流程:

1)法一:深度优先搜索:

  • 每一步的含义
    • 首先private中构造一个函数teaversal
      • 深度优先搜索也是递归,递归三部曲
        • ①确定函数类型是bool、void还是其他,以及函数的参数传递
          1. 如果需要搜索整棵树,但是不需要对返回值进行处理的话,就不需要返回值(113题)
          2. 如果需要搜索整棵树,也需要处理返回值,就需要返回值(236题)
          3. 如果需要搜索其中一条符合条件的路径,那么也需要返回值,返回值类型为bool(本题)
        • ②确定终止条件
          • 终止条件就是遇到叶节点,而且刚好得到了目标值,关于目标值,这里并没有像以前一样累加,判断是否相等,而是用count-的方式,当遇到叶节点&&count==0的时候终止,返回true
        • ③确定单层递归的逻辑
          • 当left、right不为空时进行递归
          • 注意这里有用到回溯,所以在递归之前count先-,递归玩需要判断是否已经返回了true,如果是,那就返回true,如果false,那就count加上减去的值
      • 最后不要忘了返回false,这是没有返回true的时候
    • 最后就是public中的主函数了
      • 先判断root,空就false
      • 直接return traversal(root,targetSum-root->val);

  • 知识点套路
    1. 深度优先搜索
    2. 不再用累加的方式来判断是否符合了,而是使用count-的方式,因为累加的话很难判断和回溯
  • 前提
  • 注意点
    • 单层递归后需要判断是否已经返回了false,是的话要加回去,已经true的话就可以直接返回最终结果了
    • 最后整个函数的末尾需要返回一个false,因为如果到了最后还没有返回的话,肯定是false,并且函数式bool类型,必须要有返回值

法二:迭代

class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if(root==NULL)return false;
        stack<pair<TreeNode*,int>>stk;
        stk.push(pair<TreeNode*,int>(root,root->val));
        while(!stk.empty()){
            pair<TreeNode*,int>node=stk.top();
            stk.pop();

            if(node.first->left==NULL && node.first->right==NULL && targetSum==node.second)return true;

            if(node.first->right){
                stk.push(pair<TreeNode*,int>(node.first->right,node.second+node.first->right->val));
            }
            if(node.first->left){
                stk.push(pair<TreeNode*,int>(node.first->left,node.second+node.first->left->val));
            }
        }
        return false;
    }
};

注意:

和之前的迭代法差不多,最需要注意的就是这里的stack里面用了pair,注意写法加上.first什么的。

113. Path Sum II

class Solution {
private:
    vector<vector<int>>result;
    vector<int>path;
    void traversal(TreeNode*root,int count){
        if(root->left==NULL && root->right==NULL && count==0){
            result.push_back(path);
            return;
        }
        if(root->left==NULL && root->right==NULL)return;

        if(root->left){
            path.push_back(root->left->val);
            count-=root->left->val;
            traversal(root->left,count);
            count+=root->left->val;
            path.pop_back();
        }
        if(root->right){
            path.push_back(root->right->val);
            count-=root->right->val;
            traversal(root->right,count);
            count+=root->right->val;
            path.pop_back();
        }
        return;
    }
public:
    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;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值