题目

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,



return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

分析

判断给定树中有无 【跟—>叶子】路径节点值之和为 给定sum

递归实现思想:

  1. 空树,返回false
  2. 单根节点,判断,若等于sum返回true,否则返回false
  3. 更新sum -= root—>val 递归判断左右子树,其中一真即真。

AC代码

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
//空树返回false
if (root == NULL)
{
return false;
}
//若是叶子节点,判断返回
else if (!root->left && !root->right)
{
if (root->val == sum)
return true;
else
return false;
}
else
{
//否则,递归判断左右子树
sum -= root->val; return hasPathSum(root->left, sum) || hasPathSum(root->right, sum);
}
}
};

GitHub测试程序源码

LeetCode(112) Path Sum的更多相关文章

  1. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  2. LeetCode(112):路径总和

    Easy! 题目描述: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及 ...

  3. LeetCode(307) Range Sum Query - Mutable

    题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...

  4. LeetCode(304)Range Sum Query 2D - Immutable

    题目 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  5. LeetCode(303)Range Sum Query - Immutable

    题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...

  6. LeetCode(40) Combination Sum II

    题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...

  7. LeetCode(1)Two Sum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  8. LeetCode(39) Combination Sum

    题目 Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C w ...

  9. Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)

    Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...

随机推荐

  1. MFC 创建UI线程

    对于windows来说,所有的线程都是一样的,但MFC却把线程区分为两种:用户界面(UI)线程和工作者线程.用户界面线程具有消息循环而工作者线程没有.UI线程可以创建窗口并给这些窗口发送消息,工作者线 ...

  2. 转 Linux SendMail发送邮件失败诊断案例(四)

    http://www.cnblogs.com/kerrycode/p/7826036.html

  3. Unity Shader入门精要学习笔记 - 第17章 Unity的表面着色器探秘

    转自 冯乐乐的<Unity Shader 入门精要> 2010年的Unity 3 中,Surface Shader 出现了. 表面着色器的一个例子. 我们先做如下准备工作. 1)新建一个场 ...

  4. PHP知识点总结3

    PHP 函数 PHP 的真正威力源自于它的函数. 在 PHP 中,提供了超过 1000 个内建的函数. <html> <body> <?php function writ ...

  5. ASP.NET Core MVC/WebAPi 模型绑定

    public class Person { public string Name { get; set; } public string Address { get; set; } public in ...

  6. vue对象和视图

    1 Vue框架 1. vue 与 jQuery 区别 jQuery 仍然是操作DOM的思想, 主要jQuery 用来写页面特效 Vue是前端框架(MVVM) ,对项目进行分层. 处理数据 2 前端框架 ...

  7. Objective-C Runtime Reference

    This document describes the OS X Objective-C 2.0 runtime library support functions and data structur ...

  8. shell框架

    #!/bin/bash#注释#注释#环境变量相关,如下PATH=/sbin:/bin:/usr/bin:/usr/sbin #引入库函数,如下,类似于c语言的#include "*.h&qu ...

  9. vue.js学习总结

    下面使用的命令工具为git bash 使用命令行工具搭建vue.js项目 vue.js官网命令行工具安装 为了提升安装速度,建议将 npm 的注册表源设置为国内的镜像 1.输入命令:npm insta ...

  10. 电脑连接海信电视 HDMI

    注意:我们家的电视是海信的,所以不能代表所有的电视哦~~~ 家里电视有线电视已经过期很长时间了,早就想把电脑连接到电视上用电视做显示器的心了,今天来兴趣了,就弄了一下!!! 用电脑连接电视需要先解决两 ...