[leetcode] 4. 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 and
sum = 22,5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1return true, as there exist a root-to-leaf path
5->4->11->2which sum is 22.
首先这个树很奇怪。。。。我也不知道他是怎么插进去的值的,不像传统二叉树那样有大小判断插入,而是好像在。。。随机插入。。。当然这个我们不管,题目是要问找一条从根到叶的路径加下来sum能否等于给的sum。
如果二叉树基本功熟练的话,可以直接看出来这就是一个深度优先的搜索,然后这个是前序遍历加个和就行了。解法如下:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void PreSum(TreeNode *temp, int sum, int tmp, bool &flag)
{
if (temp != NULL)
{
if (temp->val + tmp == sum && temp->left == NULL && temp->right == NULL)
flag = true;
else
{
tmp += temp->val;
PreSum(temp->left, sum, tmp, flag);
PreSum(temp->right, sum, tmp, flag);
}
} } bool hasPathSum(TreeNode *root, int sum) {
bool flag = false;
PreSum(root, sum, 0, flag);
return flag;
}
};
因为这个递归弹出实在没法跟要求一样,所以我加了个flag作为判断。
[leetcode] 4. Path Sum的更多相关文章
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 112. Path Sum 路径和
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 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- LeetCode 437. Path Sum III (路径之和之三)
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance
引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
随机推荐
- oracle用户具有的权限和角色
如何查看一个oracle用户具有的权限和角色 1.查看所有用户: select * from dba_users; select * from all_users; select * from use ...
- URLDownloadToFileW
[ilink32 Error] Error: Unresolved external 'URLDownloadToFileW' referenced from D:\USERS\PUBLIC\DOCU ...
- Unknown picture file extension
Image1.Picture.LoadFromFile('aaa.jpg'); Project Project1.exe raised exception class EInvalidGraphic ...
- docker学习记录1
起因 现在自己学习微服务,服务器越来越多,虽然自己写了一些shell脚本来安装需要的软件,比如mysql,redis,jdk等等,但是还是好麻烦.希望学习docker能够快速安装部署这些东西. 记录一 ...
- Python实现七牛云视频播放
这篇文章是使用Python的Web框架Django Rest Framework来提供视频相关的api接口,主要功能包括视频上传.视频转码.视频访问授权.删除视频文件.视频截图功能. 七牛云上的基本概 ...
- S初始化生产环境数据
一.将开发机的库文件导出10.10.1.139开发机服务器,桌面上的BAT文件,将数据库表结构和表数据导出来,导到E:\Repository,设置SADMIN密码永不过期BAT文件内容如下: ::导出 ...
- 用java实现一个简易编译器2-语法解析
- 02- 画文字和图片-------------之前写的那个微博项目,可以试试用画图片的方式来处理,这样应该比UILabel 代码少点,一会试试
1.画图片 - (void)drawRect:(CGRect)rect { // Drawing code UIImage *image = [UIImage imageNamed:@"pa ...
- js中直接对字符串转义-用于solr ulr 关键词转义
js代码 /* * 获取UTC格式的字符串,参数必须是 */var solrDateFormat = function (o){ var date; if(typeof o == 'str ...
- char a[] = "hello world1"和char *p = "hello world2";的区别(转)
转自:jianchi88 http://blog.csdn.net/jianchi88/article/details/6876405 #include<stdio.h> int main ...