翻译

给定一个二叉树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. Clickomania(区间DP)

    描述 Clickomania is a puzzle in which one starts with a rectangular grid of cells of different colours ...

  2. Radio Transmission(bzoj 1355)

    Description 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知道它的最短长度是多少. Input 第一行给出字符串的长度,1 < L ≤ 1, ...

  3. localStorag的一点见解

    dot方法对localStorag方法进行键值操作 设值 localStorage.hello = 'world'; localStorage.zhangsan = 'lisi'; 取值: var v ...

  4. Linux 下tomcat的配置

    参考文档:http://blog.csdn.net/jeamking/article/details/7881196 http://www.cnblogs.com/panxuejun/p/618641 ...

  5. android中bitmap图片与二进制,String间的转化

    1, public Bitmap stringtoBitmap(String string) {                 // 将字符串转换成Bitmap类型                 ...

  6. spring boot -- 无法读取html文件,碰到的坑

    碰到的坑,无法Controller读取html文件 1. Controller类一定要使用@Controller注解,不要用@RestController 2. resource目录下创建templa ...

  7. 利用javascript实现在圆周上匀速划动的动画效果

    先看下效果:          

  8. codeforces #472(div 1)

    B(two point) 题意: 给出长度为n的非递减数组E[1..n],对于所有三元组(i,j,k),1<=i<j<k<=n且Ek-Ei<=U,我们需要计算出最大的(E ...

  9. ganglia-monitoring-centos-linux

    http://www.tecmint.com/install-configure-ganglia-monitoring-centos-linux/ http://monitor.gitlab.net/ ...

  10. Go -- go语言指针

    package main import "fmt" type Test struct { Name string } func change2(t *Test) { t.Name ...