LeetCode OJ 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 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.
Subscribe to see which companies asked this question
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int flag = false;
void DFS(struct TreeNode *root, int sum){
if(root == NULL){
return;
}
){
flag = true;
}
DFS(root->left, sum - root->val);
DFS(root->right, sum - root->val);
}
bool hasPathSum(struct TreeNode* root, int sum) {
flag = false;
DFS(root, sum);
return flag;
}
LeetCode OJ 112. Path Sum的更多相关文章
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- 【LeetCode OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Path Sum
Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...
- 【一天一道LeetCode】#112. Path Sum
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ 113. Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- 【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 ...
- LeetCode OJ:Path Sum II(路径和II)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- LeetCode OJ: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】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- oracleclient连oracle库 报System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本
在iis下发布eworkflow+eform+ebiao的代码,访问oracle的数据库,用oracleClient或者oledb的方式连接,有时会报“System.Data.OracleClient ...
- 查看Oracle加锁情况及解锁方法
注意:必须有Oracle DBA 权限才能操作一下SQL语句: ---查询锁定的会话 select * from v$session t where t.SID in (select t2.sessi ...
- Android安装apk时报错:INSTALL_FAILED_NO_MATCHING_ABIS
问题背景 OS:无关 AS:无关 Genymotion:2.5.2 Virtual Device:Google Nexus 5 - 5.1.0 - API 22 原因分析 CPU架构不符 解决方案 对 ...
- linux基础3——与XP共享文件夹的设置
导出linux文件有一般有三种方式: 1.类似windows下的文件直接拖拽,鼠标选中目标文件,从linux文件目录下直接拉至windows文件目录下: 2.U盘拷贝导出到Windows文件目录下:同 ...
- Xilinx DCM 使用---- 输出频率问题
最近在使用Xilinx FPGA验证项目,使用DCM将50M晶振分频得到20M时钟.但是下载代码到板子上验证,发现板子完全不工作. 然后 测量时钟,发现根本就没有20M时钟.查找资料,以及跟以前项目对 ...
- Servlet基础
今天在学习Servlet的时候遇到了一个问题:大概是这样java.lang.ClassNotFoundException: HelloServlet at org.apache.catalina.lo ...
- Excel with COM
COM excelApplication ; COM workBooks ; COM workSheets ; COM workSheet ; COM work ...
- Salesforce 数据清洗
新系统上线后,需要导入历史数据,但是旧数据格式,数据缺失,数据错误,奇异值,属性归类与新系统有很大的gap.因此我们需要建立一套数据动态清洗规则给Salesforce系统,通过这些规则自动清洗导入数据 ...
- 最新原创:Power8伴随云计算横空出世
Power8伴随云计算横空出世 自从IBM将并不赚钱的x86服务器业务转手给联想之后,从此IBM便开始了硬件转型的新里程碑.伴随此事继续发酵的还有Power System Linux落户北京,从此IB ...
- 谈谈Ruby中的类变量
Ruby中的类变量,很多文章都是不太建议使用的,主要原因在于他的一些特性容易导致犯一些错误,尤其在广泛使用元编程的时候. 初步接触类变量可能觉得他跟C++的类静态成员和Java中的静态变量没什么区别, ...