LeetCode_Path Sum
一.题目
Path Sum
Total Accepted: 57762 Total Submissions: 193808My
Submissions
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.
二.解题技巧
三.实现代码
#include <iostream> /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ 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)
{
if (!root)
{
return false;
} if (!root->left && !root->right && root->val == sum)
{
return true;
} int SumChild = sum - root->val; if (hasPathSum(root->left, SumChild))
{
return true;
} if (hasPathSum(root->right, SumChild))
{
return true;
} return false;
}
};
四.体会


LeetCode_Path Sum的更多相关文章
- 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 - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- 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 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [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] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- Tsinsen 拉拉队排练
建回文树,然后判断长度奇偶性,统计下来排序即可. 题目链接:http://www.tsinsen.com/ViewGProblem.page?gpid=A1255 By:大奕哥 #include< ...
- 2017haoi总结
暴力都写不对的蒟蒻QAQ 现在只看了上午的第二题.. AM.T2 写了40分的记忆化搜索,最差复杂度大概是n^3,100以下应该是稳过的..通过递归返回[l+1,r]的答案,l=r特判,int函数 ...
- 【贪心大水题】BZOJ3410-[Usaco2009 Dec]Selfish Grazing 自私的食草者
[题目大意] 给出n个区间,问最多选取多少个区间使得它们互相不重叠. [思路] 水题quq改善心情用.按照右端点大小排序,每次更新上一次的右端点,如果当前左端点大于上次右端点可取. #include& ...
- 性能测试工具——Jmeter使用小结(一)
Apache Jmeter是针对Java的一款性能测试工具,利用该工具可以实现自动化的批量测试和结果聚合,适合做接口压测.今天就来捋一捋软件安装的一些小细节和使用. 一.安装 Jmeter基于JDK, ...
- C++ -- STL泛型编程(一)之vector
STL提供三种组件:容器,迭代器,算法,它们都支持泛型程序设计标准容器有两类:顺序容器和关联容器. 顺序容器(vector,list,deque,string等)是一系列元素的有序组合. 关联容器(s ...
- Codeforces Round #305 (Div. 1) A. Mike and Frog 暴力
A. Mike and Frog Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pr ...
- 2015 UESTC 搜索专题A题 王之迷宫 三维bfs
A - 王之迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...
- 用C++/CLI搭建C++和C#之间的桥梁(二)—— 基本语法
托管对象的创建和引用 在前文中我们已经演示过创建一个托管对象,对于如下C#代码: System.Object x = new System.Object(); 其在C++/CLI中的等价代码如下: S ...
- JavaScript中数组的各种操作方法
[监测数组] 使用instanceof操作符,进行检测 ar arr = [1,2,3]; // arr = '非非'; if(arr instanceof Array){ console.log(' ...
- Activex 数字签名
本次使用makecert的命令如下: makecert -sv online.pvk -n "CN=中国在线" -ss My -r -b 01/01/1900 -e 01/01/9 ...