[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) 有关 这种情况下,时间 ...
随机推荐
- Rhythmk 一步一步学 JAVA(4):Spring MVC -之拦截器
1.实现拦截器类(myInterceptor): package com.rhythmk.Interceptor; import javax.servlet.http.HttpServletReque ...
- leetcode260
public class Solution { public int[] SingleNumber(int[] nums) { var dic = new Dictionary<int, int ...
- win7+jdk环境变量配置
进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置:1.下载jdk(http://java.sun.com/javase/downloads/index.jsp),我下载的版本是: ...
- spring与mybatis
- Force SDK to create Appointment programmatically in crm 2013 onPremise
As I know based on this: msdn.microsoft.com/.../gg334289.aspx and community.dynamics.com/.../book-an ...
- python grpc
pip install grpcio pip install grpcio-tools python -m grpc_tools.protoc -I. --python_out=. --grpc_py ...
- 108.Convert Sorted Array to Binary Search Tree(Array; Divide-and-Conquer, dfs)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 思路 ...
- 554. Brick Wall最少的穿墙个数
[抄题]: There is a brick wall in front of you. The wall is rectangular and has several rows of bricks. ...
- sourcetree免注册方法
step1: https://www.sourcetreeapp.com/官网下载windows版软件 step2: 右键-->以管理员身份运行,便安装成功了 step3: 安装好之后会有这么一 ...
- linux常用的一些命令行操作(ubuntu)
软件安装 sudo apt-get install xxx 压缩和解压缩 1. *.tar 用 tar –xvf 解压 2. *.gz 用 gzip -d或者gunzip 解压 3. *.tar.gz ...