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 ...
随机推荐
- R语言基础篇——数据读写
1.键盘输入数据(适合小数据集) #创建一个指定模式但不含数据的变量 mydata<-data.frame(age=numeric(0),gender=character(0),weight=n ...
- django信号相关
Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. 1.Django内置信号 Model signals pre_in ...
- web框架的本质(使用socket实现的最基础的web框架、使用wsgiref实现的web框架)
import socket def handle_request(client): data = client.recv(1024) client.send("HTTP/1.1 200 OK ...
- DAG
DAG的生成 DAG(Directed Acyclic Graph) 叫做有向无环图,原始的RDD通过一系列的转换就形成了DAG,根据RDD之间的依赖关系的不同将DAG划分成不同的Stage,对于窄依 ...
- There is no Action mapped for namespace [/] and action name [TestAction] ass
1.修改action的name值 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE strut ...
- PayPal支付对接
开发时间:2019-04-30 我的目标:在我们公司的海外网站上,接入PayPal支付,美国用户在线完成付款. 准备: (1)准备:公司注册信息(执照,注册号,法人等),法人信息(身份证,住址等) ( ...
- 有关shell中冒号的特殊用法
有关shell中冒号的特殊用法,供朋友们参考. : ${VAR:=DEFAULT} 当变量VAR没有声明或者为NULL时,将VAR设置为默认值DEFAULT.如果不在前面加上:命令,那么就会把${VA ...
- js中关键字 const , let , var 的用法区别
1.const定义的变量不可以修改,而且必须初始化. 2.var定义的变量可以修改,如果不初始化会输出undefined,不会报错. 3.let是块级作用域,函数内部使用let定义后,对函数外部无影响 ...
- selenium 自动化的坑(1)
UI自动化,一天一坑系列(1) 不要试图自作聪明添加一些不必要的东西. 个人遇到的问题关于下拉框的,本来一个下拉框连续点击两次就好了,结果自己多余将谷歌的提示‘正在受到自动化控制’去掉了,导致原本很简 ...
- C++数组读入MATLAB数据
data = rand(8, 10); fid = fopen('File.data', 'w'); if fid == - 1 error('Cannot open file for writing ...