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 ...
随机推荐
- django 中间件的使用??
django 中的中间件(middleware),在django中,中间件其实就是一个类,在请求到来和结束后,django会根据自己的规则在合适的时机执行中间件中相应的方法. 在django项目的se ...
- 【推荐系统】知乎live入门
参考链接: 知乎推荐系统live:姚凯飞推荐系统live 目录 1.推荐概览与框架 2.细节补充 3.召回 4.排序 5.常用技能与日常工作 5.用户画像-特征工程 6.相关经验 7.推荐考试拿分路径 ...
- Echarts数据可视化grid直角坐标系(xAxis、yAxis)详解:
mytextStyle={ color:"#333", //文字颜色 fontStyle:"normal", //italic斜体 oblique倾斜 font ...
- 使用Tabulator遇到的问题
1.Tabulator好像是不支持ie,按照Tabulator文档引入,打开浏览器总是报缺少文件,换了谷歌果然好了. 2.编辑某一行的数据 代码: //Build Tabulator var tabl ...
- nginx报错[error] CreateFile() "D:\Java-windows\nginx-1.16.0/logs/nginx.pid" failed (2: The system cannot find the file specified)
无论是nginx -s stop还是nginx -s reload命令,都会出现这个错误. 解决方法:使用命令创建/logs/nginx.pid文件,命令如下所示: nginx -c conf/ngi ...
- ssh免口令密码登录及兼容性处理
1). client ---> server 客户端发起对服务器的连接,登录服务器. 2). 须在客户端生成密钥对 注意: 公钥加密私钥解:私钥加密公钥解. 可以发布公钥,但私钥是不能出本机的. ...
- Java虚拟机——Class类文件结构
Class文件格式采用一种类似C语言结构体的结构来存储数据,这种数据结构只有两种数据类型:无符号数和表. 无符号数属于基本的数据类型,数据项的不同长度分别用u1, u2, u4, u8表示, ...
- BZOJ1135 LYZ(POI2009) Hall定理+线段树
做这个题之前首先要了解判定二分图有没有完备匹配的Hall定理: 那么根据Hell定理,如果任何一个X子集都能连大于等于|S|的Y子集就可以获得完备匹配,那么就是: 题目变成只要不满足上面这个条件就能得 ...
- phpStorm 配置PHP_CodeSniffer自动检查代码
环境 ubuntu18.4 phpstorm php7.2 最正确安装方法 sudo apt-get install php-codesniffer 一.composer安装PHP_CodeSniff ...
- web项目分层设计
model.dao.service.controller之间的关系,还有util和task的简介 model: 与数据库中的表一一对应,实现set和get的方法.