LeetCode 112 Path Sum(路径和)(BT、DP)(*)

简介: 版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50569025 翻译给定一个二叉树root和一个和sum,决定这个树是否存在一条从根到叶子的路径使得沿路所有节点的和等于给定的sum。
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50569025

翻译

给定一个二叉树root和一个和sum,

决定这个树是否存在一条从根到叶子的路径使得沿路所有节点的和等于给定的sum。

例如:
给定如下二叉树和sum=225
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1
返回真,因为这里存在一条根叶路径(5->4->11->2),它的和为22

原文

Given a binary tree and a sum, 

determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

分析

如果是一个二叉搜索树,那么可以根据它的特性来做一些减法走一下捷径。

先用最基本的方法来求解试试:

bool hasPathSum(TreeNode* root, int sum) {
    if (!root) return false;
    if (!root->left && !root->right) return root->val == sum;
    return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}

然后我在想,如果在某个节点上的和已经超过了sum,那应该就false了吧。然后加了一行:

if (root->val > sum) return false;      

噢不……原来还可以有负数的,错了。算了不指望这个了,继续改着玩……

其实这个和上面的差不多,差别在于上面的判断方法是不断的用给定的sum减掉当前的值,而这个是不断往上累加看能否累计到刚好等于sum,还引入了额外的变量……

bool dfs(TreeNode* root, int pasum, int sum) {
    if (!root) return false;
    if (!root->left && !root->right) return pasum + root->val == sum;
    return dfs(root->left, pasum + root->val, sum) || dfs(root->right, pasum+root->val, sum);
}

bool hasPathSum(TreeNode* root, int sum) {
    return dfs(root, 0, sum);
}

代码

/**
* 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:
    bool dfs(TreeNode* root, int pasum, int sum) {
        if (!root) return false;
        if (!root->left && !root->right)   return pasum + root->val == sum;
        return dfs(root->left, pasum + root->val, sum) || dfs(root->right, pasum + root->val, sum);
    }
    bool hasPathSum(TreeNode* root, int sum) {
        return dfs(root, 0, sum);
    }
};
目录
相关文章
|
2天前
leetcode代码记录(二叉树的所有路径
leetcode代码记录(二叉树的所有路径
11 0
|
2天前
|
机器人
leetcode代码记录(不同路径 II
leetcode代码记录(不同路径 II
8 0
|
2天前
|
机器人
leetcode代码记录(不同路径
leetcode代码记录(不同路径
10 0
|
2天前
leetcode热题100.二叉树中的最大路径和
leetcode热题100.二叉树中的最大路径和
20 0
|
2天前
|
vr&ar
leetcode热题100.路径总和 III
leetcode热题100.路径总和 III
20 1
|
2天前
|
算法 C++
【刷题】Leetcode 1609.奇偶树
这道题是我目前做过最难的题,虽然没有一遍做出来,但是参考大佬的代码,慢慢啃的感觉的真的很好。刷题继续!!!!!!
9 0
|
2天前
|
算法 索引
【刷题】滑动窗口精通 — Leetcode 30. 串联所有单词的子串 | Leetcode 76. 最小覆盖子串
经过这两道题目的书写,相信大家一定深刻认识到了滑动窗口的使用方法!!! 下面请大家继续刷题吧!!!
12 0
|
2天前
|
算法
【刷题】 leetcode 面试题 08.05.递归乘法
递归算法是一种在计算机科学和数学中广泛应用的解决问题的方法,其基本思想是利用问题的自我相似性,即将一个大问题分解为一个或多个相同或相似的小问题来解决。递归算法的核心在于函数(或过程)能够直接或间接地调用自身来求解问题的不同部分,直到达到基本情况(也称为基础案例或终止条件),这时可以直接得出答案而不必再进行递归调用。
25 4
【刷题】 leetcode 面试题 08.05.递归乘法
|
2天前
|
存储 算法 安全
【刷题】 leetcode 面试题 01.06 字符串压缩
来看效果: 非常好!!!过啦!!!
25 5
【刷题】 leetcode 面试题 01.06 字符串压缩