[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->2
which 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 ...
随机推荐
- RPC框架的服务注册和发现
https://www.cnblogs.com/valor-xh/p/6281502.html https://blog.csdn.net/listslim1/article/details/5157 ...
- C#获取外网IP、本机MAC地址及Ping的实现
原文 获取外网IP, C#获取本机的MAC地址,C#通过编程方式实现Ping 获取外网IP地址 思路是通过WebRequest连接一些网上提供IP查询服务的网站,下载到含有你的IP的网页,然后用正则表 ...
- 基于 DirectX11 的 MMDViewer 04-渲染目标视图和多视口
上篇文章给出了一个简单并且可以运行的渲染框架,接下来将介绍框架中的渲染管线构成. 1.创建渲染管线 在你创建完一个窗口后,接着便要创建渲染管线,使用的函数是 D3D11CreateDeviceAndS ...
- ELK-Stack 最后一次全篇文档
简介: ELK-Stack 日志收集系统.最后一次全篇记录的笔记,之后关于 ELK 的笔记都将是片段型.针对性的. 环境介绍: ELK-Stack:192.168.1.25 ( Redis.LogS ...
- shell编程——日志输出的同时显屏
在执行脚本的时候我们常常需要将执行过程全部输出到日志里,以备出现报错时可以跟踪分析,开始我用的是exec: exec 1>info.log #把全部执行过程输出到info日志中 exec 2&g ...
- 编译gcc5.1.0时的报错
编译安装gcc5.1.0时出现如下报错: configure: error: error verifying int64_t uses long long 这是由于没有安装gcc_c++导致的,安装下 ...
- space defender,太空版植物大战僵尸 游戏基本框架的设计
- SpringBoot整合SpringData和Mysql数据库
1.新建maven项目(具体的新建过程就不细说了) 2.添加maven依赖,也就是在pom.xml文件添加项目的依赖jar包: <project xmlns="http://maven ...
- ubuntu14.04 64 位 vmware tools 问题
当提示说open-vm-tools版本太低时可以这样解决 1.sudo apt-get autoremove open-vm-dkms open-vm-tools --purge 2.安装vmware ...
- 关于BigDecimal小记
昨天在写一个关于金额计算的时候,随手用了BIgDecimal结果出问题了,如下图NO.3那样,期望值是10.00,结果是10.1... 后来发现犯了一个想当然的错误,那就是两个参数的构造方法是这样的, ...