翻译

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

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

比如:
给定例如以下二叉树和sum=22。
5
/ \
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);
}
};

LeetCode 112 Path Sum(路径和)(BT、DP)(*)的更多相关文章

  1. [LeetCode] 112. Path Sum 路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  2. [leetcode]112. Path Sum路径和(是否有路径)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  3. LeetCode 112. Path Sum路径总和 (C++)

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  4. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  5. [LeetCode] 112. Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  6. [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)

    Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...

  7. LeetCode 112. Path Sum (二叉树路径之和)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  8. LeetCode 112. Path Sum 二叉树的路径和 C++

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  9. LeetCode 112. Path Sum(路径和是否可为sum)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

随机推荐

  1. RAC+单节点搭建DG

    primary RAC to single standby 参考文献:RAC+单实例DATAGUARD 配置   http://blog.csdn.net/miyatang/article/detai ...

  2. linux移动硬盘unmount报错处理

    备份数据用的移动硬盘unmount时报: 通过fuser命令来kill掉设备进程,再unmount移动设备 先看fuser命令帮助信息: [root@athenadb2 ~]# fuser -help ...

  3. HDU 1693 Eat the Trees ——插头DP

    [题目分析] 吃树. 直接插头DP,算是一道真正的入门题目. 0/1表示有没有插头 [代码] #include <cstdio> #include <cstring> #inc ...

  4. charts 画饼图

    统计某一天某类物体的百分比 新知识点:aggregate https://blog.csdn.net/congcong68/article/details/51619882 主要的 $group $m ...

  5. POJ 2096 (dp求期望)

    A - Collecting Bugs Time Limit:10000MS     Memory Limit:64000KB     64bit IO Format:%I64d & %I64 ...

  6. MarsEdit 快速插入代码

    <div class="cnblogs_Highlighter"> <pre class="brush:objc;gutter:true;"& ...

  7. Delphi使用进行post数据时超时设置

    因项目需要进行http的post提交数据,开始时用indy的idHttp组件,但是测试时发现当网络中断(如拔掉网线),idHttp的超时设置无效果,要等20秒才提示超时(参考网上的做法,将indy9升 ...

  8. Codeforces Round #511 (Div. 2) C. Enlarge GCD

    题目链接 题目就是找每个数的最小素因子,然后递归除,本来没啥问题,结果今天又学习了个新坑点. 我交了题后,疯狂CE,我以为爆内存,结果是,我对全局数组赋值, 如果直接赋值,会直接在exe内产生内存,否 ...

  9. win10+Linux18.04双系统安装

    给好多可爱的妹子重装了那么多次电脑,懒得码过程,因为我一般每次都要查一查...这次来个综合版吧,超简单,无脑操作. 首先说一下我的电脑Thinkpad + 500G 硬盘 (2014年买的老电脑) 首 ...

  10. (5)Swing事件

    import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; publi ...