[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 ...
随机推荐
- 20155214 2016-2017-2 《Java程序设计》第3周学习总结
20155214 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 Chapter4 一个原始码中可以有多个类定义,但只能有一个公开类,且文档中的主文档名必须与 ...
- 20155222 卢梓杰 myod
20155222 卢梓杰 myod 复习c文件处理内容 编写myod.c 用myod XXX实现Linux下od -tx -tc XXX的功能 main与其他分开,制作静态库和动态库 编写Makefi ...
- C语言复习20170728
C语言复习20170728 键盘输入和屏幕输出 字符常量:把字符放在一对单引号内,适用于多数可打印字符. 转义字符: 以反斜线()开头,也是放在一对单引号内,适用于控制字符. .\t,是水平制表符,相 ...
- 20155323 2016-2017-2 《Java程序设计》第9周学习总结
20155323 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操 ...
- 20145226夏艺华 《Java程序设计》预备作业3
安装虚拟机 上学期开学的时候就安装了Linux虚拟机,由于我的是Mac OS,所以和windows下的安装有所不同. 我使用的是VirtualBoxVM虚拟机,稳定性还不错,需要的同学可以从https ...
- sql语句-4-子查询
- PostgreSQL的checkpoint能否并行
对于此问题,在社区进行了提问,并得到了一些大牛的解答: http://postgresql.1045698.n5.nabble.com/Can-checkpoint-creation-be-paral ...
- VS2010 不显示 最近使用的项目 解决办法
昨天重装了VS2010,然后开了项目看了下今天早上再打开发现起始页近使用项目列表是空白的,每次打开项目都要去到指定目录去找解决方案才能打开,感觉很麻烦,在网上找了下解决方案,解决步骤下:菜单 —— 运 ...
- 用Micro:bit做剪刀、石头、布游戏
剪刀.石头.布游戏大家都玩过,今天我们用Micro:bit建一个剪刀.石头.布游戏! 第一步,起始 当你摇动它时,我们希望the micro:bit选择剪刀.石头.布.尝试创建一个on shake b ...
- 【转】glumer Appium + Python环境搭建(移动端自动化)
最近整理了一下自动化的东西,好久没搭建环境又踩了不少坑,appium的环境搭建比较繁琐,好多同行估计都在环境上被卡死了.分享一下~~ 一.安装JDK,配置JDK环境 百度搜索下载就行,这里分享一 ...