Level:

  Easy

题目描述:

You are given a binary tree in which each node contains an integer value.

Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

Example:

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1 Return 3. The paths that sum to 8 are: 1. 5 -> 3
2. 5 -> 2 -> 1
3. -3 -> 11

思路分析:

  由于题目中所说的路径,不局限于从根到叶子节点,任何一个节点都可以作为路径的开始节点和终止节点,所以我们以根节点作为开始节点,查找和为sum的路径数,然后分别以根的左孩子和右孩子为起始节点去查找和为sum的路径数,依次递归向下推导,得到最终的结果。

代码:

/**public class TreeNode{
int vla;
TreeNode left;
TreeNode right;
public TreeNode(int val){
this.val=val;
}
}*/
public class Soulution{
public int pathSum(TreeNode root,int sum){
if(root==null)
return 0;
int res=0;
res=pathCheck(root,sum);
res=res+pathSum(root.left,sum);
res=res+pathSum(root.right,sum);
return res;
}
public int pathCheck(TreeNode root,int sum){
if(root==null)
return 0;
int count=0;
if(sum==root.val)//当sum等于root.val时证明存在一条路径和为sum
count++;
count=count+pathCheck(root.left,sum-root.val);
count=count+pathCheck(root.right,sum-root.val);
return count;
}
}

9.path Sum III(路径和 III)的更多相关文章

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

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

  3. [Leetcode] Path Sum 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 113. Path Sum II路径总和 II (C++)

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

  5. [LeetCode] 931. Minimum Falling Path Sum 下降路径最小和

    Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...

  6. 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...

  7. 112. Path Sum二叉树路径和

    [抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

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

  9. 437 Path Sum III 路径总和 III

    给定一个二叉树,二叉树的每个节点含有一个整数.找出路径和等于给定数的路径总数.路径不需要从根节点开始,也不需要在叶节点结束,当路径方向必须是向下的(只从父节点到子节点).二叉树不超过1000个节点,节 ...

  10. [LeetCode] Minimum Path Sum 最小路径和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

随机推荐

  1. 实验吧CTF题库-密码学(部分)

    这里没有key: 打开链接,有一个弹窗 然后就是一个空白网页,右键查看源代码 这里有一串js密文,解密一下,https://www.dheart.net/decode/index.php 得到flag ...

  2. [MySQL]表创建外键失败:ERROR 1005 (HY000): Can't create table (errno: 150)

    在数据库中建立一个新表(表引擎为InnoDB)时, 需要用到外键, 所以就在建表的时候加了一句foreign key (column) references table_name.但是执行时出现 ER ...

  3. 第四章 Javac编译原理(待续)

    Javac是什么 Javac编译器的基本结构 Javac工作原理分析 设计模式解析之访问者模式

  4. python ConfigParser 读取配置文件

  5. Delphi XE2 新控件 布局Panel TGridPanel TFlowPanel

    Delphi XE2 新控件 Firemonkey 布局Panel Windows平台VCl TGridPanel TFlowPanel FMX 跨平台 TLayout TGridLayout TFl ...

  6. XE Button Color

    XE Button Color,FMX Button 颜色 也可以放个rectangle+Glyph控件. http://blogs.embarcadero.com/sarinadupont/2014 ...

  7. import configparser

  8. WCF客户端调用并行最大同时只支持两个请求

    做项目的时候发现 频繁调用WCF服务时 明明一次性发起了几十个请求 而在服务端记录的日志却显示出现了排队的迹象 并且都是最大并发数为2 在网上狂搜 大家给出来的解决方法都是增加web.config里面 ...

  9. CentOS7下安装pip和pip3

    1.首先检查linux有没有安装python-pip包,直接执行 yum install python-pip 2.没有python-pip包就执行命令 yum -y install epel-rel ...

  10. Angular22 HttpClient的使用

    1 HttpClient介绍 HttpClient时Http的演进,注意:Http在@angular/http中,而HttpClient在@angular/common/http中: 使用前需要在模块 ...