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 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的更多相关文章
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- 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 ...
- 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 ...
- 437. Path Sum III
原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...
- 第34-3题:LeetCode437. Path Sum III
题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- 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 ...
- LeetCode算法题-Path Sum III(Java实现)
这是悦乐书的第227次更新 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第94题(顺位题号是437).您将获得一个二叉树,其中每个节点都包含一个整数值.找到与给定值相加的路径数 ...
- [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 ...
随机推荐
- 攻防世界WEB高手进阶之Zhuanxv
1.一开始就是一个时钟界面 2.扫描目录发现/list 目录 打开是后台登陆,看了一下源码,也没发现什么,焦灼... 3.百度上搜了一波wp,发现原来在css里面藏了东西 后台的背景图片居然是这样读取 ...
- 从输入 URL 到页面展示到底发生了什么?
1.输入地址 当我们开始在浏览器中输入网址的时候,浏览器其实就已经在智能的匹配可能得 url 了,他会从历史记录,书签等地方,找到已经输入的字符串可能对应的 url,然后给出智能提示,让你可以补全ur ...
- fastjson将json格式字符串转成list集合
1.gameListStr = "[{"gameId":"1","gameName":"哈哈"},{" ...
- 项目Alpha冲刺--7/10
项目Alpha冲刺--7/10 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Alpha冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合 ...
- 10、Python迭代器与生成器(iterator、for循环、generator、yield)
一.迭代器(foreach) 1.可迭代的对象 内置有__iter__方法的都叫可迭代的对象. Python内置str.list.tuple.dict.set.file都是可迭代对象. x = 1._ ...
- CentOS7.6编译安装redis5.0
yum install gcc wget http://download.redis.io/releases/redis-5.0.0.tar.gz tar xvf redis-5.0.0.tar.gz ...
- SparkSQL读写外部数据源--csv文件的读写
object CSVFileTest { def main(args: Array[String]): Unit = { val spark = SparkSession .builder() .ap ...
- 多项式求逆入门 板题(Luogu P4238)
下面是代码,推导详见 传送门 模板Code #include <cstdio> #include <cstring> #include <algorithm> us ...
- 关于System.AccessViolationException异常
什么是AccessViolationException 试图读写受保护内存时引发的异常. 继承 Object Exception SystemException AccessViolationExce ...
- Windows异常分发函数---KiUserExceptionDispatcher
简介 KiUserExceptionDispatcher 是SEH分发器的用户模式的负责函数.当一个异常发生的时候,该异常将生成一个异常事件,内核检查该异常是否是由于执行用户模式代码导致的.如果是这样 ...