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 ...
随机推荐
- 小结一下:javascript 金额计算
今天在项目中开发一个计算金额的功能,开始我是这样做的: eg: var amount += parseFloat( 0.01+0.02); 结果:0.0300000000000000001 为什么呢? ...
- sax/dom/jdom/dom4j的区别
sax/dom/jdom/dom4j的区别[转] 博客分类: xml 网上能够查到很多的4种解析方式的区别,我再做一下摘录和总结,顺带给自己做个备份. SAX sax分析器在对xml文档进行分析时 ...
- Android基础
今天学习Android基本环境及基础知识,正确来说是重新温习Android知识,因为初次接触Android开发已经是两年前的事,如今又回到Android开发依然那么熟悉,依然可以让人很兴奋,Andro ...
- 删除项目中的.svn信息
有时候我们新开发一个项目时,会将以前的项目从svn上down下来,然后复制一份.这样就会有个问题,项目中的svn信息就会一直存在.下面介绍删除方法: 1.新建一个.txt的文档.然后将下面代码粘贴到文 ...
- oracle数据库中的基本语句
下面的都是最基本的oracle数据库的数据查询语句,这是我在网上整理的一份文档,方便以后自己的查看,当然,能把这些记下来就是最好的. 说明:查询表中的数据 1. select * from emp; ...
- Bash注释
1)单行注释: #!/bin/bash #echo "Martin" echo "Martin" #echo "Martin" 2)多行注释 ...
- Linux:history命令记录操作时间、操作用户、操作IP
[步骤] 1./etc/profile文件中加入以下内容 2.执行:source /etc/profile [效果]
- 用浏览器模拟各种User Agent
转至:http://www.cnblogs.com/top5/archive/2012/06/07/2540686.html 测试页面的时候经常需要不同的User Agent,Firefox.Chro ...
- dubbo-admin重新编译后部署出现 valid LOC header (bad signature)异常
前段时间从网上下载dubbo-admin发现在jdk1.7下不可用,后来根据网上说的升级了jar包版本还是有问题,于是观察了编译发现编译时出现类似如下异常: --------------------- ...
- 在centos配置nginx+php的环境
环境版本:centos6.8/nginx1.10.2/php5.6.29 ====================安装nginx1.10.2==================== #根据centos ...