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. 开源JS代码前面加!,+的意义

    我们都知道,函数的声明方式有这两种 function fnA(){alert('msg');}//声明式定义函数 var fnB = function(){alert('msg');}//函数赋值表达 ...

  2. 通过id查询出图片

    第一步,model中需要如下的做法 [UIHint("Picture")] //加上之后会默认显示上传图片的模式 public int PictrueId { get; set; ...

  3. 用UltraISO把硬盘文件制作成ISO格式

    转自:https://wenku.baidu.com/view/0052c88dcc22bcd126ff0cbf.html 用UltraISO把硬盘文件制作成ISO格式方法: 制作硬盘ISO文件步骤一 ...

  4. UML在实践中的现状和一些建议

    本文是我在csdn上看到的文章,由于认识中的共鸣,摘抄至此. 原文地址:http://blog.csdn.net/vrman/article/details/280157 UML在国内不少地方获得了应 ...

  5. sublime3 There are no packages available for installation

    我的是网上下载的绿色版 1.找到sublime\Data\Packages  删除Packages control相关的文件夹和文件 下载https://packagecontrol.io/Packa ...

  6. Struts2框架04 struts和spring整合

    目录 1 servlet 和 filter 的异同 2 内存中的字符编码 3 gbk和utf-8的特点 4 struts和spring的整合 5 struts和spring的整合步骤 6 spring ...

  7. 关于Java继承体系中this的表示关系

    Java的继承体系中,因为有重写的概念,所以说this在子父类之间的调用到底是谁的方法,或者成员属性,的问题是一个值得思考的问题; 先说结论:如果在测试类中调用的是子父类同名的成员属性,这个this. ...

  8. 转:c语言学习笔记 二进制和十进制的互相转化

    http://www.cnblogs.com/xkfz007/articles/2590472.html

  9. php学习笔记-POST和GET的区别

    POST和GET都可以用来提交数据. POST方法提交的数据在浏览器的地址栏是不可见的,当然利用一些工具是可以看到的,而GET方法提交的数据在地址栏是可见的.两者比较,POST更安全一点. POST方 ...

  10. Android之对话框Dialog

    首先是确认对话框 //确认对话框 private void showLog1() { AlertDialog.Builder dialog = new AlertDialog.Builder(this ...