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

题目标签:Tree
  这道题目给了我们一个二叉树,和一个sum,让我们找到有多少条path 的和是等于sum的,这里的path不一定要从root 到底,可以是中间的点开始到下面的点。需要另外设两个functions。
  traverseTree function - preOrder 遍历每一个点,然后把每一个点代入到findPathSum function。
  findPathSum function - 找到从root 开始到各个点的 path sum, 和sum比较,一样的话,就res++。
 
  利用traverseTree 来把每一个点(不同的level)代入findPathSum, 来找到从那个点开始往下的各种path sum,这样就可以包含 中间的点开始到下面的点的这种可能性。而且不会重复,因为每次代入的点,都是唯一的,而且找的path sum都是从这个点起始的。这两个funciton 就相当于 for loop 中间 再包括一个for loop, 来遍历array。
 
 

Java Solution:

Runtime beats 68.27%

完成日期:07/06/2017

关键词:Tree

关键点:利用两个递归functions来搜索从每一层level 各点开始到下面层次点的path值

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
int res = 0;
public int pathSum(TreeNode root, int sum)
{
if(root == null)
return res; traverseTree(root,sum); return res;
} public void traverseTree(TreeNode node, int sum)
{
if(node == null)
return; findPathSum(node, 0, sum);
traverseTree(node.left, sum);
traverseTree(node.right, sum); } public void findPathSum(TreeNode node, int path_sum, int sum)
{
if(node == null)
return; if(path_sum + node.val == sum)
res++; findPathSum(node.left, path_sum + node.val, sum);
findPathSum(node.right, path_sum + node.val, sum); }
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

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

  4. leetcode:Path Sum (路径之和) 【面试算法题】

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  5. 437 Path Sum III 路径总和 III

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

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

  7. LeetCode 437. Path Sum III (STL map前缀和)

    找遍所有路径,特判以根为起点的串即可. 代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tr ...

  8. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  9. 【leetcode】437. Path Sum III

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

随机推荐

  1. informix服务端口和oralce服务端口

    查找informix的服务端口1>>more .profile 找到: INFORMIXDIR=/home/informix INFORMIXSERVER=aaaa2>>cd ...

  2. Intellij IDEA WEB结构目录说明【转载】

    https://my.oschina.net/lujianing/blog/186737?p=1#OSC_h2_1

  3. 举例让抽象问题具体化:包含min函数的栈

    定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数.在该栈中,调用min.push及pop的时间复杂度都是O(1). import java.util.Stack; public c ...

  4. Visual studio code快捷键

    {"key": "escape", "command": "cancelSelection", "when&q ...

  5. (二)Java数组特性总结,你真的了解数组吗?

    一.数组的特殊性 (一)数组标识符是一个引用,指向堆中创建的一个真实对象,这个对象(数组)保存了指向保存其他对象的引用. (二)数组中保存引用类型时保存的是对象引用,基本数据类型数组保存基本数据的值. ...

  6. easyui动态生成列

    需求:一个id对应多个key value 将id作为标识列 key值作为表头 value作为值显示.数据表可分为两张表 param数据表: 下表一个id对应上表多个key及value 如下图 id_p ...

  7. TCP/IP笔记

    TCP/IP 连接 三次握手 TCP/IP 四次分手 @TODO TIME_WAIT 状态 有三种状态可以进入此状态 1.由FIN-WAIT-2,双方不同时发起FIN,主动关闭的一方在完成自身发起的关 ...

  8. PyTorch教程之Tensors

    Tensors类似于numpy的ndarrays,但是可以在GPU上使用来加速计算. 一.Tensors的构建 from __future__ import print_function import ...

  9. eclipse通过maven构建web项目步骤说明

    1.  File -> New -> Other ,搜索maven,选择Maven Project,点击Next 2.这里不需要改继续Next 3.这里需要注意,需要选择maven-arc ...

  10. 分享基于分布式Http长连接框架--架构模型

    我画了个简单的架构图来帮助说明: 其实为发布订阅架构模式. 生产者和消费者我们统一可理解为客户端,消息中间件可认为是服务端. 生产者和消费者做为客户端要跟服务端交互,则先通过代理订阅服务端,订阅成功后 ...