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
 class Solution {

     public int pathSum(TreeNode root, int sum) {
if(root==null) return 0;
int res = path(root,sum)+pathSum(root.right,sum)+pathSum(root.left,sum);
return res; } private int path(TreeNode root, int sum) {
int res=0;
if(root==null)
return res ;
if(sum==root.val)
res+=1;
res+=path(root.left,sum-root.val);
res+=path(root.right,sum-root.val);
return res;
} }

437. Path Sum 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 437 Path Sum III 路径和

      相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...

  3. 437 Path Sum III 路径总和 III

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

  4. 47. leetcode 437. Path Sum III

    437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...

  5. 437. Path Sum III

    原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...

  6. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  7. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

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

  9. 【LeetCode】437. Path Sum III 解题报告(Python)

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

随机推荐

  1. 严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderList

    最近在用idea部署war文件的时候,总是出现了部署失败的错误,刚开始并没有在意,但是现在次数越来越多了,不得不在意了,然后就在百度上搜,然后就有了各种说法 1,错误的信息是: One or more ...

  2. Ajax实现异步上传图片

    要求:点击页面浏览按钮后,选择需要上传的图片,页面无刷新,将上传的图片展示出来 开发流程 一:在页面编写表单代码和js代码 <!DOCTYPE html PUBLIC "-//W3C/ ...

  3. js indexof用法indexOf()定义和用法

    indexOf()定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. 语法 stringObject.indexOf(searchvalue,fromindex) ...

  4. .NET程序调试技巧(一):快速定位异常的一些方法

    作为一个程序员,解BUG是我们工作中常做的工作,甚至可以说解决问题能力是一个人工作能力的重要体现.因为这体现了一个程序员的技术水平.技术深度.经验等等. 那么在我们解决BUG的过程中,定位问题是非常重 ...

  5. BlockingQueue(阻塞队列)分析

    如果读者还有一点印象,我们在实现线程池时,用了队列这种数据结构来存储接收到的任务,在多线程环境中阻塞队列是一种非常有用的队列,在介绍BlockingQueue之前,我们先解释一下Queue接口. Qu ...

  6. (转)Unity笔记之编辑器(UnityEditor)

    在使用unity3d的过程中,时常会需要从场景中寻找或者调用一个对象,而Unity就提供了一个贴心的功能——拖拽.用鼠标拖一下中比写堆代码直观的多吧!但是Unity提供的远远不止这一丢丢,下面我们来简 ...

  7. 振铃效应(ringing artifacts)

    artifacts    纰漏 个人总结不一定对:图像复原中损失高频信息的话会产生振铃效应. 理想低通滤波器在频率域的形状为矩形,那么其傅立叶逆变换在时间域为sinc函数 图像处理中,对一幅图像进行滤 ...

  8. poj 1129(dfs+图的四色定理)

    题目链接:http://poj.org/problem?id=1129 思路:根据图的四色定理,最多四种颜色就能满足题意,使得相邻的两部分颜色不同.而最多又只有26个点,因此直接dfs即可. #inc ...

  9. poj 3422(最小费用最大流)

    题目链接:http://poj.org/problem?id=3422 思路:求从起点到终点走k次获得的最大值,最小费用最大流的应用:将点权转化为边权,需要拆点,边容量为1,费用为该点的点权,表示该点 ...

  10. 彻底解决 webpack 打包文件体积过大

    http://www.jianshu.com/p/a64735eb0e2b https://segmentfault.com/q/1010000006018592?_ea=985024 http:// ...