前言

 

【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);
}
}
};
作者:Double_Win
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则作者保留追究法律责任的权利。  若本文对你有所帮助,您的关注推荐是我们分享知识的动力!

[LeetCode 题解]:Path Sum的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

  7. 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 ...

  8. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  9. [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 ...

  10. 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...

随机推荐

  1. 解决办法 Field userService in com.sxsj.controller.RegistLoginController required a bean of type

    转自:https://blog.csdn.net/awmw74520/article/details/82687288 APPLICATION FAILED TO START Error starti ...

  2. MPEG-2码流结构分析

    MPEG2视频编码定义在 ISO/IEC13818-2中,MPEG2 video sequence如下图所示 我们可以借助Elecard Stream Analyer工具来分析MPEG2视频码流 MP ...

  3. python web框架简介Bottle Flask Tornado

    Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. ? 1 2 3 4 pip inst ...

  4. Scala基础:数组(Array)、映射(Map)、元组(Tuple)、集合(List)

    数组 package com.zy.scala object ArrayDemo { def main(args: Array[String]): Unit = { //定长数组 val arr1 = ...

  5. 解剖Nginx·模块开发篇(3)ngx_http_hello_world_module 模块的基本函数实现

    还记得我们定义过一个结构体如下吗? typedef struct { ngx_str_t output_words; } ngx_http_hello_world_loc_conf_t; 它就是 He ...

  6. 这几天用高通VUFORIA的体会

    VUFORIA 主要用来做图像识别,先把图片上至网站,然后网站分析生成数据包 在UNITY中导入VUFORIA SDK和数据包后,就可以正常使用了 对了,数据包需要勾选Load Active那个选项, ...

  7. POJ 3017 DP + 单调队列 + 堆

    题意:给你一个长度为n的数列,你需要把这个数列分成几段,每段的和不超过m,问各段的最大值之和的最小值是多少? 思路:dp方程如下:设dp[i]为把前i个数分成合法的若干段最大值的最小值是多少.dp转移 ...

  8. Java-精确计算工具类

    import java.math.BigDecimal; import java.math.RoundingMode; /** * 精确计算工具类(加,减,乘,除,返回较大值,返回较小值) */ pu ...

  9. SUSE制作ISO源

    These commands have been tested on openSUSE 11. First create a directory where you will store your I ...

  10. ubuntu14.04 64 位 vmware tools 问题

    当提示说open-vm-tools版本太低时可以这样解决 1.sudo apt-get autoremove open-vm-dkms open-vm-tools --purge 2.安装vmware ...