437. Path Sum III

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
package leetcode.easy;

/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class PathSumIII {
int count = 0; private void helper(TreeNode root, int sum) {
if (root == null) {
return;
} if (root.val == sum) {
count++;
} if (root.left != null) {
helper(root.left, sum - root.val);
} if (root.right != null) {
helper(root.right, sum - root.val);
}
} public int pathSum(TreeNode root, int sum) {
if (root == null) {
return count;
}
helper(root, sum);
if (root.left != null) {
pathSum(root.left, sum);
}
if (root.right != null) {
pathSum(root.right, sum);
}
return count;
} @org.junit.Test
public void test() {
int sum = 8;
TreeNode tn11 = new TreeNode(10);
TreeNode tn21 = new TreeNode(5);
TreeNode tn22 = new TreeNode(-3);
TreeNode tn31 = new TreeNode(3);
TreeNode tn32 = new TreeNode(2);
TreeNode tn34 = new TreeNode(11);
TreeNode tn41 = new TreeNode(3);
TreeNode tn42 = new TreeNode(-2);
TreeNode tn44 = new TreeNode(1);
tn11.left = tn21;
tn11.right = tn22; tn21.left = tn31;
tn21.right = tn32;
tn22.left = null;
tn22.right = tn34; tn31.left = tn41;
tn31.right = tn42;
tn32.left = null;
tn32.right = tn44;
tn34.left = null;
tn34.right = null; tn41.left = null;
tn41.right = null;
tn42.left = null;
tn42.right = null;
tn44.left = null;
tn44.right = null;
System.out.println(pathSum(tn11, sum));
}
}

LeetCode_437. Path Sum III的更多相关文章

  1. 【leetcode】437. Path Sum III

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

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

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

  4. 437. Path Sum III

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

  5. 第34-3题:LeetCode437. Path Sum III

    题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

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

  8. LeetCode算法题-Path Sum III(Java实现)

    这是悦乐书的第227次更新 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第94题(顺位题号是437).您将获得一个二叉树,其中每个节点都包含一个整数值.找到与给定值相加的路径数 ...

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

随机推荐

  1. 基于JFinal中搭建wopi协议支撑办法

    1.添加maven依赖 <dependency> <groupId>com.github.icecooly</groupId> <artifactId> ...

  2. P2606 [ZJOI2010]排列计数

    P2606 [ZJOI2010]排列计数 因为每个结点至多有一个前驱,所以我们可以发现这是一个二叉树.现在我们要求的就是以1为根的二叉树中,有多少种情况,满足小根堆的性质. 设\(f(i)\)表示以\ ...

  3. Jmeter连接mysql,如何用delete、update、insert真正删除、更改、插入数据库里的数据;

    1.如下图,当插入数据的时候如图对应填写,查询数据的时候上面插入的那条数据就会显示,但是如果不执行下图的提交数据:到数据库里查的时候,插入的这条数据实际上并没有插入成功: . 结果:如果没有提交数据, ...

  4. python测试开发django-rest-framework-63.基于函数的视图(@api_view())

    前言 上一篇讲了基于类的视图,在REST framework中,你也可以使用常规的基于函数的视图.它提供了一组简单的装饰器,用来包装你的视图函数, 以确保视图函数会收到Request(而不是Djang ...

  5. .NET Core项目修改project.json来引用其他目录下的源码等文件的办法 & 解决多框架时 project.json 与 app.config冲突的问题

    作者: zyl910 一.缘由 项目规模大了后,经常会出现源码文件分布在不同目录的情况,但.NET Core项目默认只有项目目录下的源码文件,且不支持“Add As Link”方式引入文件.这时需要手 ...

  6. Java——CaptchaUtil生成二维码乱码

    前言 这个问题就是因为Linux上没有字体,你可以有两种方法,一个在生成的时候设置字体,一个就是安装字体. 默认的字体为Courier 乱码情况 步骤 安装字体工具 yum install -y fo ...

  7. 域权限维持:Ptk(Pass The Ticket)

    Kerberos协议传送门:https://www.cnblogs.com/zpchcbd/p/11707302.html 1.黄金票据 2.白银票据

  8. LeetCode 1039. Minimum Score Triangulation of Polygon

    原题链接在这里:https://leetcode.com/problems/minimum-score-triangulation-of-polygon/ 题目: Given N, consider ...

  9. js form 表单使用

    <!--表单所有标签--> <form action="" method="" name=""> 单行文本框:< ...

  10. 原生js打地鼠

    我们要做的是一个打地鼠的游戏,只用原生js 1.导入需要的图片 2.编写页面css样式demo.css *{ margin:0; padding:0; } .game{ position: relat ...