LeetCode 112 Path Sum(路径和)(BT、DP)(*)
翻译
给定一个二叉树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)(*)的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- float.h
float.h 一背景知识 浮点算术非常复杂 很多小的处理器在硬件指令方面甚至不支持浮点算术 其他的则需要一个独立的协处理器来处理这种运算 只有最复杂的计算机才在硬件指令集中支持浮点运算 ...
- P2341 受欢迎的牛
受欢迎的牛 洛谷链接 题目大意: 有n头牛,牛会喜欢其他牛和自己,而喜欢是可以传递的,求被所有牛喜欢的牛的数量. 思路: 这是一道tarjan缩点的题目.被所有牛都喜欢的牛,一定会在一个强连通分量里. ...
- 两周多学完Java 23种设计模式
最近两周任务不是很繁重,对于一个刚入职4个月的菜鸟来说,学习设计模式并灵活使用简直天方夜谭:但是当我询问我导师需要学点啥的时候?“<Java设计模式>,这个必须要学”,一句简单粗略的 ...
- BZOJ 1012 [JSOI2008]最大数maxnumber【线段树】
水题,每次记录一下当前有多少个数,然后按照题目所指示的那样模拟就行,每次向线段树末尾插入(其实是修改)题目中指定的数,然后询问当前的个数到前面Q个数中最大值是多少结果就是,好久不碰线段树了,用数组模拟 ...
- ElasticSearch 中 REST API 详解
本文主要内容: 1 ElasticSearch常用的操作 2 ElasticSearchbulk命令 ES REST API elasticsearch支持多种通讯,其中包括http请求响应服务,因此 ...
- tensorflow加载embedding模型进行可视化
1.功能 采用python的gensim模块训练的word2vec模型,然后采用tensorflow读取模型可视化embedding向量 ps:采用C++版本训练的w2v模型,python的gensi ...
- CodeForces 379D 暴力 枚举
D. New Year Letter time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- iOS 取应用版本
// 应用网址 返回字典中有多种数据 NSString *urlString2 = [NSString stringWithFormat: @"%@", @"http: ...
- Scrapy学习-4-Items类&Pipelines类
items类使用 作用 能使得我们非常方便的操作字段名 在items.py中定制我们的类 class ArticleItem(scrapy.Item): title = scrapy.Field() ...
- android Containers控件
1.RadioGroup 一组单选框容器 2.ListView 3.GridView 4.ExpandableListView 可折叠列表 5.ScrollView 上下滚动条 6.Horizonta ...