前言

 

【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. wwwroot文件无读取某个文件夹权限(对IIS_USER增加全部权限)

  2. LNK2026: 模块对于 SAFESEH 映像是不安全的<转>

    转自VC错误:http://www.vcerror.com/?p=162 错误描述: 在使用VS2012编译工程时,提示错误:" error LNK2026: 模块对于 SAFESEH 映像 ...

  3. rtmp一些状态信息详解-as连接FMS服务器报错状态汇总~~

    原地址:http://help.adobe.com/zh_CN/AIR/1.5/jslr/flash/events/NetStatusEvent.html 下表说明了 code 和 level 属性可 ...

  4. 为什么是static?

    为什么是static因为系统开始执行一个程序前,并没有创建main()方法所在类的实例对象,它只能通过类名类调用主方法. public static void main(String args[])我 ...

  5. javascript变量,作用域和内存问题

    1:ECMAScript所有函数的参数都是按值传递的 function setName(obj){ obj.name="finn"; obj=new Object(); obj.n ...

  6. margin+absolute布局:右栏固定主内容自适应 demo

    margin+absolute布局:右栏固定主内容自适应 demo 头部 Aside侧边栏区域 Main主内容区域 底部 #demo{width:80%;margin:auto;height:300p ...

  7. jQuery——表单异步提交

    如果不做任何处理,表单提交时会刷新页面,为了改善体验,可以使用jQuery做到异步提交表单:通过$("#form").serialize()将表单元素的数据转化为字符串,然后通过$ ...

  8. c3p0、dbcp和proxool比较

    现在常用的开源数据连接池主要有c3p0.dbcp和proxool三种,其中: hibernate开发组推荐使用c3p0; spring开发组推荐使用dbcp(dbcp连接池有weblogic连接池同样 ...

  9. mesos in docker

    docker pull mesosphere/mesos-master:1.4.0 docker pull mesosphere/mesos-slave:1.4.0 在Docker中运行Mesos的推 ...

  10. Solo and Mute

    [Solo and Mute ] Muting means a transition will be disabled. Soloed transtions are enabled and with ...