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. 【vijos】1892 树上的最大匹配(树形dp+计数)

    https://vijos.org/p/1892 这个必须得卡评测机+手动开栈才能卡过QAQ 手动开栈我百度的... int size=256<<20; //256MB char *p=( ...

  2. [Hadoop]安装

    1 从官网下载hadoop稳定版 http://www.apache.org/dyn/closer.cgi/hadoop/common/ 2 安装JAVA 参考如下blog http://www.cn ...

  3. MathType可以编辑物理公式吗

    很多的物理专业的人都在为编辑物理公式头疼,其实要写出这些公式并不难,要写出这些物理公式,那你就需要一个MathType公式编辑器!这是一款专业的公式编辑器,不管多复杂的公式或方程,都可以用它编辑出来, ...

  4. PHP和JS判断手机还是电脑访问

    当用户使用手机等移动终端访问网站时,我们可以通过程序检测用户终端类型,如果是手机用户,则引导用户访问适配手机屏幕的移动站点.本文将介绍分别使用PHP和JAVASCRIPT代码判断用户终端类型. PHP ...

  5. HasMany() = (1..*) HasOptional() = (1..0,1) HasRequired() = (1..1)

    http://www.cnblogs.com/yeagen/archive/2012/10/15/2724237.html

  6. phpstorm 内存设置

    https://blog.csdn.net/qq_33862644/article/details/81938970

  7. Maven使用deploy上传jar包到远程库 以Oracle驱动为例

    一.首先要得到Oracle JDBC Driver 1.通过Oracle官方网站下载相应版本:http://www.oracle.com/technetwork/database/features/j ...

  8. iOS-将NSString转换成UTF8编码的NSString

    在使用网络地址时,一般要先将url进行encode成UTF8格式的编码,否则在使用时可能报告网址不存在的错误,这时就需要进行转换 下面就是转换函数: NSString *urlString= [NSS ...

  9. 5秒后跳转到另一个页面的js代码

    今天看视频学习时学习了一种新技术,即平时我们在一个页面点击“提交”或“确认”会自动跳转到一个页面. 在网上搜了一下,关于这个技术处理有多种方法,我只记下我在视频里学到的三种: 1.用一个respons ...

  10. 【BZOJ4624】农场种植 FFT

    [BZOJ4624]农场种植 Description 农夫约翰想要在一片巨大的土地上建造一个新的农场. 这块土地被抽象为个 R*C 的矩阵.土地中的每个方格都可以用来生产一种食物:谷物(G)或者是牲畜 ...