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 andsum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path5->4->11->2
which
sum is 22.
检查一颗二叉树是否有和某数相等的路径。和前面的最少深度二叉树思路差不多。
1 每进入一层,如果该层跟节点不为空,那么就加上这个值。
2 如果到了叶子节点,那么就与这个特定数比较,相等就说明存在这样的路径。
当然可以不是用递归优化一点算法吧。是用循环的话就可以在找到值的时候直接返回。递归也可以增加判断,找到的时候每层加判断迅速返回,不过感觉效果也高不了多少。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
bool judge = false;
minD(root, sum, judge);
return judge;
}
void minD(TreeNode *node, int overall, bool& judge, int levelNum = 0)
{
if(node == nullptr) return;
levelNum += node->val;
minD(node->left, overall,judge, levelNum);
minD(node->right, overall,judge, levelNum);
if(node->left==nullptr && node->right==nullptr && overall==levelNum)
judge = true;
}
};
//2014-2-17 update
bool hasPathSum(TreeNode *root, int sum)
{
if (!root) return false;
if (!root->left && !root->right && root->val == sum) return true;
if (hasPathSum(root->left, sum - root->val)
|| hasPathSum(root->right, sum - root->val)) return true;
}
分享到:
相关推荐
Leetcode two sum java 解法
双指针算法,python数组双指针算法求和问题LeetCode2sum3sum4sum含代码
文档python数组双指针算法求和问题LeetCode2sum3sum4sum含代码提取方式是百度网盘分享地址
主要介绍了LeetCode -- Path Sum III分析及实现方法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
oj.leetcode题解, 2sum, 运用hashtable解决这到问题,时间复杂度O(N)
目录目录计算机系学生为什么要刷算法PAT乙级和甲级是什么考PAT对学习 | 考研 | 工作的作用是什么刷PAT乙级和甲级需要掌握哪些知识关于PAT刷题中编程语言
LeetCode — Path Sum III分析及实现方法 题目描述: You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need...
leetcode 2sum leetCode解决方案 我对 leetCode 问题的解决方案 已解决的练习列表: 简单的: 2sum - 带有未排序的数组; O(n) 复杂度; 花了~3ms 2sum - 使用排序数组; O(n) 复杂度 罗马转整数; O(n) 复杂度 合并...
leetcode 2SUM 使用以前的地图最快的 2Sum O(N) 使用 unordered_map 比 map 快 以前的地图 là gì ? 上一张地图 đơn giản là 地图 nhưng thay vì ta cần một vòng for để khởi tạo 地图 thì ta khởi...
leetcode 2sum 2总和 力码研究1
3sum 力码 力扣算法 (注:“ :locked: " 表示你需要来自 Leetcode) # 标题 解决方案 困难 001 简单的 002 中等的 003 中等的 004 难的 005 中等的 006 简单的 007 简单的 008 简单的 009 简单的 010 难的 011 中等...
leetcode 2sum # Programming-Problems This will have many problems from all over the web, As of writing this readme there is Haskell 99: 28 finished + Huffman Coding LeetCode: LC1: 2Sum [Easy] LC2: Add...
3sum leetcode-curation-topical 精选 Leetcode 问题,按主题/概念分类。 我的策展标准是问题必须是有价值的,而不仅仅是为了难而难。 有价值的问题通常可以以不同的时间/空间效率(通过使用各种数据结构)以多种...
用java实现的求迷宫最短路径的算法源代码,代码中有大量注释,容易看懂
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
Swif-LeetCode-Utils:在LeetCode上快速创建和打印ListNode和TreeNode的方式
LeetCode 101:和你一起你轻松刷题(C++)
leetcode 轮廓 1_count_and_say.cpp - super_ugly_number.cpp - Detect_Pattern.cpp - degree_of_array.cpp - 键盘.cpp - 2Sum_Data_Structure_Design.cpp - shuffle_array.cpp - permutations.cpp - kth_missing_...
leetcode所有题目和解答,是java版本的,但是也包括c语言版本,包括程序和题解,并且含有算法精髓,花了两个多月粗略看完的,分享给想刷题的你们。
leetcode两数之和代码 题目描述:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个...