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 ...
随机推荐
- nginx 499错误
原因: 服务响应时间太长,客户端自动断开链接. 解决: 1. 找到响应世间长的接口,看依赖的数据源(数据库,第三方接口等)响应时间是否超时,还是自己程序有逻辑问题. 可以通过加入日志打印时间消耗来确定 ...
- 多个nginx之间如何实现反向代理和负责均衡
1)nginx反向代理: http { upstream routeadmin { ip_hash; server 127.0.0.1:9201 weight= ...
- MVC的view页面内嵌C#语法发现路径被转码的解决方法
一,上视图代码,如下 console.log('@urlquery.ToString()'); console.log('@Html.Raw(urlquery.ToString())'); 二,显示结 ...
- NGUI的怎么在一个Gameobject(游戏物体)中调用另一个Gameobject(游戏物体)的脚本(C#)
一,在C#代码中,我们都知道可以给游戏物体添加一个脚本,如下图 二,在当前我们是可以调用到该游戏物体脚本定义的变量,但是我们要在其他脚本调用怎么办?如下代码, KnapSackItem kn = it ...
- IOC依赖注入的原理
一.什么是IOC 维基百科上说到:2004年Martin Fowler 提出了“控制反转的”概念,他得出的结论是:依赖对象的获得被反转了.后来为这个创造了一个更好的名字:依赖注入(IOC = Inve ...
- glob & fnmatch -- 使用Unix style通配符
通配符: ? 匹配单个字符 * 匹配 0+ 个字符 [seq] 匹配属于区间的单个字符 [!seq] 匹配不属于区间的单个字符 注意: "." just a " ...
- 07http基础
1.http协议 1.1 概念 是对浏览器和服务器端数据传输格式的规范! 1.2 http协议内容 请求 GET /bbs/hello HTTP/1.1 # 请求行 Host: localhost:8 ...
- [php代码审计] apache 后缀名解析“漏洞”
不能说是漏洞,只是 apache 特性而已. 下面是apache httpd.conf中截取的一段: <IfModule mime_module> # # TypesConfig poi ...
- git log的个性化设置
--date=(relative|local|default|iso|rfc|short|raw) Only takes effect for dates shown in human-readabl ...
- 解决安装mysql-connector-odbc-5.3.2 错误1918……不能加载安装或转换器库……的BUG
还是在虚拟机Windows Server 2003上安装mysql-connector-odbc-5.3.2,装着装着就报错了,大致是“错误1918……不能加载安装或转换器库……”,问我Retry,I ...