[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 ...
随机推荐
- eclipse中导入web项目时,出现转不了项目类型的问题解决方案
解决步骤: 1.进入项目目录,可看到.project文件,文本编辑器打开. 2.找到<natures>...</natures>代码段,加入如下标签内容并保存: <nat ...
- C# HTTP请求GET,POST
转自原文 [C#]HTTP请求GET,POST HTTP定义了与服务器交互的不同方法,基本方法有GET,POST,PUT,DELETE,分别对于查,该,增,删.一般情况下我们只用到GET和POST,其 ...
- A generic error occurred in GDI+的解决方案
转自智慧光原文A generic error occurred in GDI+. 解决方法 使用image1.RotateFlip(RotateFlipType.Rotate90FlipNone)方法 ...
- rtmp聊天相关归总
使用共享对象(SharedObject)来开发时时文字聊天其实是很简单的. SharedObject可以跟踪和广播消息,连接到SharedObject中的其中任何一个客户端改变了SharedObjec ...
- UGUI 自动布局的重叠BUG
1,父级使用了verticalLayout(注意没有ContentSizeFilter),子级使用了ContentSizeFilter时,点击Apply常常发现,本来布局好的UI突然重叠到了一起,或位 ...
- Django 使用体会
最近急于赶项目,少有更新博文.如今项目大致不那么赶了,终于可以在晚上码字码文章,而不是码代码了. 从开始使用Django开发到现在, 也已经有大半年了.公司的项目也是逐步地加功能,加模块,一步步完善设 ...
- springboot 的定时任务使用
定时任务在Spring Boot中的集成 在启动类中加入开启定时任务的注解: 在SpringBoot中使用定时任务相当的简单.首先,我们在启动类中加入@EnableScheduling来开启定时任务. ...
- 从零玩转JavaWeb系列7web服务器-----表单的提交
在企业开放中如何提交一张表单? 例如:用户注册表 <!--表单内容--> <form action="/28-mystore/regist" id="R ...
- Oracle-11g 回缩表高水位
回缩表高水位的意义: 所有的 Oracle 段都有一个在段内容纳数据的上线,即高水位线(high water mark).HWM 是一个标记,很像水库的丽水最高水位,即使表内数据全部删除,HWM 也还 ...
- 【BZOJ3998】弦论 【后缀自动机】
题意 给定一个长度为n的字符串,求他的第k小子串是什么. 分析 T=0的时候,这个题跟SPOJ-SUBLEX的做法一样,当T=1的时候,不同位置的子串算多个,那么初始化的时候d[u]=cnt[u],没 ...