[LeetCode] PathSum
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.
思路:DFS.时间复杂度O(n), 空间复杂度O(lgN)
相关题目:《剑指offer》面试题25
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
if (root == NULL) {
return false;
}
if (root->left == NULL && root->right == NULL) {
return root->val == sum;
}
return hasPathSum(root->left, sum - (root->val)) ||
hasPathSum(root->right, sum - (root->val));
}
};
[LeetCode] PathSum的更多相关文章
- leetcode — path-sum
/** * Source : https://oj.leetcode.com/problems/path-sum/ * * * Given a binary tree and a sum, deter ...
- Leetcode::Pathsum & Pathsum II
Pathsum Description: Given a binary tree and a sum, determine if the tree has a root-to-leaf path su ...
- 递归 & 分治算法深度理解
首先简单阐述一下递归,分治算法,动态规划,贪心算法这几个东西的区别和联系,心里有个印象就好. 递归是一种编程技巧,一种解决问题的思维方式:分治算法和动态规划很大程度上是递归思想基础上的(虽然实现动态规 ...
- leetcode 38:path-sum
题目描述 给定一个二叉树和一个值sum,判断是否有从根节点到叶子节点的节点值之和等于sum的路径, 例如: 给出如下的二叉树,sum=22, 5 / ...
- path-sum leetcode C++
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
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 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] Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- 20155217 实验四 Android程序设计
20155217 实验四 Android程序设计 任务一: 完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学号. R.java文件是定义该项目所有资源的 ...
- 20155327李百乾 Exp3 免杀原理与实践
20155327李百乾 Exp3 免杀原理与实践 实践guocheng 一.Msfvenom使用编码器 1.利用(virustota)[https://www.virustotal.com/]检测实验 ...
- 2016-2017-2 20155338 实验二《Java面向对象程序设计》实验报告
2016-2017-2 20155338 实验二<Java面向对象程序设计>实验报告 实验内容: 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握 ...
- webUploader实现大文件分片,断点续传
问题: 公司现在的业务需求是要上传一个大文件,上一次写了一篇博客,做了一个简单的文件上传,支持单文件,大型文件上传 现在对之前的上传进行优化,支持断点续传,秒传功能 上次博客:[http://www. ...
- slqite3练习
连接 import sqlite3 con = sqlite3.connect(":memory:") c = con.cursor() # Create table c.exec ...
- Android 7.1.1系统源码下载、编译、刷机-Nexus 6实战
想成为一位合格的Android程序员或者一位Android高级工程师是十分有必要知道Android的框架层的工作原理,要知道其工作原理那么就需要阅读Android的源代码. 想要阅读Android的源 ...
- mongod 安装
mongod --logpath F:\mongo\db\logs\logs.log --logappend --dbpath F:\mongo\db\data --directoryperdb -- ...
- info信息的获取
一.绝对路径(_SERVER[“SCRIPT_FILENAME”])这个是最常用,也是最有效的一个办法,找到phpinfo()页面可以直接找到网站的绝对路径,对于写shell和信息搜集是必不可少的.二 ...
- Quartz学习--三 Hello Jdbc Quartz! 和 demo 结尾
四. Hello JDBC Quartz! JDBC方式: 就是说通过数据库的jdbc链接来进行quartz的一个配置 Quartz支持了很好的支持 demo用例 使用mysql作为例子进行演示 相比 ...
- lxd&openstack-lxd源码剖析
lxd:https://linuxcontainers.org/lxd/,目标是融入到openstack体系被管理,像虚拟机一样被管理使用.从如下图可知,并非走的是libvirt-lxc路线,而是no ...