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 ...
随机推荐
- 【BZOJ 3043】 3043: IncDec Sequence (差分)
3043: IncDec Sequence Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 589 Solved: 332 Description 给 ...
- Java 请求webServce接口 带参数
public String getWebServiceByParams(String param){ //获取基金缴付记录 // Post请求的url,与get不同的是不需要带参数 URL postU ...
- STM32 USB VBUS 监控
OTG_FS general core configuration register (OTG_FS_GCCFG) Bit 21 NOVBUSSENS: VBUS sensing disable op ...
- shell 编译和执行java文件
编译java程序 javac test.java 执行java程序 java test 附件test.java class test{ public static void main( ...
- 传输层TCPUDP 具体解释
1.传输层存在的必要性 因为网络层的分组传输是不可靠的,无法了解数据到达终点的时间,无法了解数据未达终点的状态.因此有必要增强网络层提供服务的服务质量. 2.引入传输层的原因 面向连接的传输服务与面向 ...
- Gulp插件使用技巧
目录: 插件的安装卸载 插件使用的基本流程 拆分任务 监听 默认任务 一.插件的安装卸载 安装: npm install gulp-less --save-dev 卸载 npm uninstall g ...
- 2014 百度之星 题解 1004 Labyrinth
Problem Description 度度熊是一仅仅喜欢探险的熊,一次偶然落进了一个m*n矩阵的迷宫,该迷宫仅仅能从矩阵左上角第一个方格開始走,仅仅有走到右上角的第一个格子才算走出迷宫,每一次仅仅能 ...
- 实验3 OpenGL几何变换
转自:http://www.cnblogs.com/opengl/archive/2012/10/30/2747130.html 1.实验目的: 理解掌握一个OpenGL程序平移.旋转.缩放变换的方法 ...
- 基于 Node.js 的轻量「持续集成」工具 CIZE
CIZE 是什么? CIZE 是一个「持续集成」工具,希望能让开发人员更快捷的搭建一个完整.可靠.便捷的 CI 服务. 甚至可以像 Gulp 或 Grunt 一样,仅仅通过一个 cizefile.js ...
- 数学图形(2.2)N叶结
上一节讲的三叶结,举一反三,由三可到无穷,这一节讲N叶结 再次看下三叶结的公式: x = sin(t) + 2*sin(2*t)y = cos(t) - 2*cos(2*t) 将其改为: x = si ...