前言

 

【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. Flask之模板过滤器

    3.2 过滤器: 过滤器的本质就是函数.有时候我们不仅仅只是需要输出变量的值,我们还需要修改变量的显示,甚至格式化.运算等等,这就用到了过滤器. 过滤器的使用方式为:变量名 | 过滤器. 过滤器名写在 ...

  2. 指向“**js/shop.js”的 <script> 加载失败

    指向“”的 <script> 加载失败 找了半天没找到原因 原来是meta里面的 csp Content-Security-Policy <meta http-equiv=" ...

  3. ZooKeeper架构

    ZooKeeper服务器端运行于两种模式下:独立模式(standalone)和仲裁模式(quorum).独立模式几乎与其术语所描述的一样:有一个单独的服务器,ZooKeeper状态无法复制.在仲裁模式 ...

  4. maven-resources-plugin使用

    命令行中带参数指定${}变量值 <build> <resources> <resource> <directory>src/main/resources ...

  5. C++虚函数表理解

    一,思维模式图 二,代码验证 class A { public: A(int x) { fProtected = x; } float GetFProtected() { return fProtec ...

  6. 【转】arm和x86的区别

    来自: https://blog.csdn.net/u012513972/article/details/78349192/ 信不信,随便逮住一个人问他知不知道CPU,我想他的答案一定会是肯定的,但是 ...

  7. ios开发中view.layer.shouldRasterize = YES 的使用说明

    在做一个NavigationController push 子页面时,发现push和pop时很卡,研究了一大阵子后,发现在子页面里影响UI流畅的只有UIImageView的圆角设置:然后我就关闭了圆角 ...

  8. flex 设置换行flex-wrap

    flex 设置flex-wrap 换行 本来预想的正常情况下,代码应该如下: ul { width: 100%; display: flex; flex-wrap: wrap; li { ; widt ...

  9. 火狐浏览器的RestClient,接口测试,Post提交数据

    昨天需要测试接口是不是调通,api中本身已经集成了测试,但加了OAuth,api有没有添加头文件,Headers的地方,所以想用RESTClient的Post提交重新测试下,但是,调了好几个小时都没有 ...

  10. 使用OpenSsl自己CA根证书,二级根证书和颁发证书(亲测步骤)

    ---恢复内容开始--- 一.介绍 企业自用, 到证书机构签发证书的费用和时间等都可以省下..... SSl证书的背景功用.......(省略万字,不废话) 可以参考: SSL证书_百度百科 X509 ...