[LeetCode 题解]:Path Sum
前言
【LeetCode 题解】系列传送门: http://www.cnblogs.com/double-win/category/573499.html
1.题目描述
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.
2. 题意
给定一颗二叉树以及一个数字,请判断在此二叉树中是否存在一条从根节点出发到其中某个叶子节点的路径数值之和与给定数字的值相等。
例如,给定的二叉树如1中图所示。给定的数字为sum=22.其结果是true,这是因为存在一条root-to-leaf的路径5->4->11->2,其路径数值和=5+4+11+2=22.
3. 思路
此题是一个典型的二叉树遍历问题,很容易想到一个递归解法。
需要注意的地方:
(1)边界条件判断,二叉树为空
(2)当前节点是否为叶子节点
(3)递归表达式hasPathSum(root->left)||hasPathSum(root->right).
4: 解法
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
if(root==NULL) return false; //树为空
if(root->left==NULL && root->right==NULL){ //当前节点为叶子节点
if(sum-root->val!=0) return false;
else return true;
}else{ //当前节点不是叶子节点,递归判断其左右节点是否满足条件
return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val);
}
}
};
[LeetCode 题解]: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 Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
- 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) 有关 这种情况下,时间 ...
随机推荐
- 使用Spring MVC创建 REST API
1.REST的基础知识 当谈论REST时,有一种常见的错误就是将其视为“基于URL的Web服务”——将REST作为另一种类型的远程过程调用(remote procedurecall,RPC)机制,就像 ...
- HTTP断点续传下载的原理
frombegintoend原文HTTP断点续传下载的原理 要实现断点续传下载文件,首先要了解断点续传的原理.断点续传其实就是在上一次下载断开的位置开始继续下载,HTTP协议中,可以在请求报文头中加入 ...
- AngularJS的 $resource服务 关于CRUD操作
AngularJs 的CRUD 操作 是指对数据库的操作: CRUD其实是数据库基本操作中的Create(创建).ReadRetrieve(读取).Update(更新).Delete(删除).而这里的 ...
- 使用avalon 实现一个序列号功能
avalon"操作数据即操作DOM"的能力,让我们可以专致于业务,写出更专业,更优雅,更易维护的代码来.现在让我们看看如何实现一个序列号输入功能.它的需求以下: 每输入4个字符就跳 ...
- visjs使用小记-3.简单网络拓扑图的折叠与展开
项目需要对节点无限层级查看,大概捣鼓了下,以下demo代码可根据节点的层级顺序,通过节点双击简单实现节点的折叠与展开 <!doctype html> <html> <he ...
- C# 调用动态代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Oracle中关于DateTime的一些描述
转载自:http://www.cnblogs.com/fmxyw/archive/2008/08/26/1276850.html 在做话务报表,参考一下信息 to_date()与24小时制表示法及 ...
- linux下使用gtest框架进行c/c++单元测试
linux下使用gtest框架进行c/c++单元测试 前言 关于此次开发工具的选择,因为我最近尝试在linux下使用vim进行c/c++编程,且之前已经对vim进行了相关的配置,所以这里应作业要求直接 ...
- 解剖Nginx·自动脚本篇(5)编译器相关主脚本
在 Nginx 的自动脚本中,auto/cc目录下的所有脚本都是用于编译器相关配置使用的.Nginx的出色跨平台性(Linux.Darwin.Solaris.Win32 等)就有这些脚本的贡献.该目录 ...
- 安装了Anaconda之后,Maya运行报错,Python 找不到 Maya 的 Python 模块
以前Maya用的好好地,结果安装了Anaconda之后,maya启动以后,日志就会报错(如下),只能自主建模,不能打开以前创建的模型,也不能导入fbx,错误提示就是Maya找不到Python模块,在网 ...