翻译

给定一个二叉树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. 九度oj 题目1031:xxx定律 题目1033:继续xxx定律

    题目描述:     对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止.    请计算需要经过几步才能将n变到1,具体可见样例. 输入:     ...

  2. 算法复习——莫队算法(bzoj1878)

    题目: Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一 段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此他的 ...

  3. P1857 质数取石子 (DP,递推)

    题目描述 桌上有若干个石子,每次可以取质数个.谁先取不了,谁就输.问最少几步能赢?(一个人取一次算一步) 输入输出格式 输入格式: 第一行N,表示有N组数据 接下来N行为石子数 输出格式: 每组数据一 ...

  4. AnyChart图表仪表控件在Flex环境下使用

    AnyChart控件是一款当前流行的数据可视化解决方案,使客户可以创建交互地.生动的图表.实时仪表和地图.同时支持Flash和HTML5显示,控件提供极好的视觉外观和配色方案能够使客户根据不同的需求设 ...

  5. linux基础命令之一

    1.cpio cpio(copy in/out) 功能说明:备份文件. 语 法:cpio [-0aABckLovV][-C <输入/输出大小>][-F <备份档>][-H &l ...

  6. thinkphp框架做项目的前期配置

    ThinkPHP 目录结构说明 ThinkPHP.php:框架的公共入口文件 App:项目放置目录 Common:包含框架的一些公共文件.系统定义.系统函数和惯例配置等 Lang:系统语言文件目录 L ...

  7. 记录一下 ps命令找出线程占用cpu情况

    https://blog.csdn.net/xnn2s/article/details/11865339

  8. Angular Material & Hello World

    前言 Angular Material(下称Material)的组件样式至少是可以满足一般的个人开发需求(我真是毫无设计天赋),也是Angular官方推荐的组件.我们通过用这个UI库来快速实现自己的i ...

  9. Codeforces 961 E Tufurama

    Discription One day Polycarp decided to rewatch his absolute favourite episode of well-known TV seri ...

  10. IOS --关于粘贴板 ,剪切板 ,UILabel的复制

    在iOS中下面三个控件,自身就有复制-粘贴的功能: 1.UITextView 2.UITextField 3.UIWebView UIKit framework提供了几个类和协议方便我们在自己的应用程 ...