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. kubernetes存储之GlusterFS

    目录 1.glusterfs概述 1.1.glusterfs简介 1.2.glusterfs特点 1.3.glusterfs卷的模式 2.heketi概述 3.部署heketi+glusterfs 3 ...

  2. Linux学习21-设置定时任务crontab

    前言 做自动化测试写的脚本需设置定时任务,在指定的时间去执行,这就需要用到定时任务.之前用jenkins可以在里面设置定时任务,很好用,其实不用jenkins,在linux上也可以用crontab做个 ...

  3. fastjson异常(字符串集合转成字符串数组)

    我是在项目中,因为受到一个string类型的list集合,然后需要把这个字符串发送给前端,进行解析. 但是前端收到的是一个字符串,不能进行解析. 所以采用 ArrayUtils.clone(JSONO ...

  4. 《exception》第九次团队作业:Beta冲刺与验收准备(第三天)

    一.项目基本介绍 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 Exception 作业学习目标 1.掌握软件黑盒测试技术:2.学会编制软件项目 ...

  5. 在windows下安装cx_Oracle问题

    将 instantclient_11_2 所在的目录添加到环境变量,但是环境变量有时没有立即生效,可以复制 oci.dll(版本也要正确)到 \Python36\Lib\site-packages 目 ...

  6. HBase学习笔记之HBase原理和Shell使用

    HBase学习指南之HBase原理和Shell使用 参考资料: 1.https://www.cnblogs.com/nexiyi/p/hbase_shell.html,hbase shell

  7. ActiveMQ基础

    消息队列的作用 为什么使用ActiveMQ,不使用其他工具 下载安装包并启动 http://localhost:8161/admin/ (账号:admin:admin) Java实现步骤: // 1. ...

  8. select 与 I/O多路转接

    参考博客:http://blog.sina.com.cn/s/blog_607072980102uxcw.html I/0多路转接: 描述符表示某个I/O.构造一张有关描述符的数据表,调用select ...

  9. 04-Flutter移动电商实战-打通底部导航栏

    关于界面切换以及底栏的实现可参考之前写的一篇文章:Flutter实 ViewPager.bottomNavigationBar界面切换 1.新建4个基本dart文件 在pages目录下,我们新建下面四 ...

  10. create系列创建节点的方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...