翻译

给定一个二叉树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. Convolution Fundamental II

    Practical Advice Using Open-Source Implementation We have learned a lot of NNs and ConvNets architec ...

  2. sqlserver查询表大小

    IF OBJECT_ID('tempdb..#TB_TEMP_SPACE') IS NOT NULL DROP TABLE #TB_TEMP_SPACE GO CREATE TABLE #TB_TEM ...

  3. WebLoad XML-parser methods

    WebLOAD provides an embedded, third-party XML parser object to improve the multi-platform support fo ...

  4. BZOJ 3309 DZY Loves Math ——莫比乌斯反演

    枚举$d=gcd(i,j)$ 然后大力反演 ——来自Popoqqq的博客. 然后大力讨论后面的函数的意义即可. http://blog.csdn.net/popoqqq/article/details ...

  5. 简单题(bzoj 1683)

    Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格 ...

  6. 频繁项挖掘算法Apriori和FGrowth

    一:背景介绍 最近在公司用spark的平台做了一个购物车的推荐,用到的算法主要是FGrowth算法,它是Apriori算法的升级版,算法的主要目的是找出频繁进行一起购买的商品.本文主要介绍两个算法的背 ...

  7. linux下面MySQL变量修改及生效

    今天在访问mysql项目的时候突然报500错误,没有找到连接,因此想到mysql的连接时间. mysql> show global variables; 主要就是连接时间是28800(8小时), ...

  8. Linux 下 GCC 编译共享库控制导出函数的方法

    通过一些实际项目的开发,发现这样一个现象,在 Windows 下可以通过指定 __declspec(dllexport) 定义来控制 DLL(动态链接库)中哪些函数可以导出,暴露给其他程序链接使用,哪 ...

  9. (7)C#连DB2---oledb方式

    1安装客户端 安装DbVisualizer Free 客户端软件 2编目 用 win+r  输入 db2cmd 启动命令行 要远程操作数据库,首先要进行编目,分三个步骤: 1. 在客户端建立服务器端数 ...

  10. STL优先队列模板

    1. 优先队列 用途:按照某一个关键字对插入元素或删除元素后的数据集进行自动排序 复杂度: logN 2. 数据声明 (1)头文件:#include<queue> (2)声明:  prio ...