LeetCode 112. Path Sum 动态演示
给一个目标值,判断一棵树从根到叶子是否至少有一条路径加起来的和等于目标值
比较典型的深度优先算法。
引入一个全局变量bResult, 一旦找到一条,就不再搜索其他的了。
class Solution {
public:
void helper(TreeNode* cur, int sum, int target, bool& bResult){
if(bResult)
return;
//a(cur)
//lk("root",cur)
//a(sum)
//a(target)
//dsp
if(!cur->left && !cur->right){
bResult|= target-sum==cur->val;
}
if(cur->left)
helper(cur->left, sum+cur->val, target, bResult);
if(cur->right)
helper(cur->right, sum+cur->val, target, bResult);
}
bool hasPathSum(TreeNode* root, int sum) {
if(!root)
return false;
if(!root->left && !root->right){
return sum==root->val;
}
//ahd(root)
bool bRet=false;
//a(bRet)
if(root->left)
helper(root->left, root->val, sum, bRet);
if(root->right)
helper(root->right, root->val, sum, bRet);
//dsp
return bRet;
}
};
程序运行动态演示 http://simpledsp.com/FS/Html/lc112.html
LeetCode 112. Path Sum 动态演示的更多相关文章
- 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 二叉树的路径和
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
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 ----- java
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Java [Leetcode 112]Path Sum
题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- [Leetcode]112. Path Sum -David_Lin
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- python学习第四十八天json模块与pickle模块差异
在开发过程中,字符串和python数据类型进行转换,下面比较python学习第四十八天json模块与pickle模块差异. json 的优点和缺点 优点 跨语言,体积小 缺点 只能支持 int st ...
- python字符串的运算有哪些
python字符串的运算有哪些 1,链接符号 + 2,判断字符串是否在某个字符串中 ‘s’ in ‘this’ 返回bool 3,字符串索引 a="this a my" a[0], ...
- C#设计模式:职责链模式(Chain of Responsibility)
一,什么是职责链模式(Chain of Responsibility) 职责链模式是一种行为模式,为解除请求的发送者和接收者之间的耦合,而使多个对象都有机会处理这个请求.将这些对象连接成一条链,并沿着 ...
- 逐行读取txt文件,分割,写入txt。。。上传,下载
s = [] f = open('querylist.txt','r') #由于我使用的pycharm已经设置完了路径,因此我直接写了文件名 for lines in f: ls = lin ...
- CentOS7搭建Kafka单机环境及基础操作
前提 安装Kafka前需要先安装zookeeper集群,集体安装方法请参照我的另一篇文档. Kafka安装 下载 wget https://archive.apache.org/dist/kafka/ ...
- MySQL的删除语句
虽然现在数据库空间越来越大,但处理数据时候还是有要删除的时候,以下整理了一些最常用的删除语句. 分成两种 一个是删除指定数据,另一个删除所有数据. 一.删除指定数据 DELETE FROM 表名 WH ...
- 09.Linux系统由于不正常关机导致的分区问题
问题:Error:UNEXPECTED INCONSISTENCY: RUN fsck MANUALLY Give root password for maintenance ------------ ...
- 了解Greenplum (2)
一.目的 1. 理解Greenplum中的数据分布策略(random 和 distribution),分析不同分布策略的优劣:2. 理解查询执行中的数据广播和数据重分布,分析在何种情况下选择哪种策略, ...
- PTA 错题记录
程设期中考, 记录一下曾经做错的选择填空. 1. 2. 3. 4. 5. 6.
- APKMirror - 直接下载google play里的应用
APKMirror - Free APK Downloads - Download Free Android APKs #APKPLZ https://www.apkmirror.com/