Path Sum I

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

      5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) return false;
if (root.left == null && root.right == null && root.val == sum) return true;
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}

Path Sum II

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

For example:
Given the below binary tree and sum = 22,

              5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

return

[
[5,4,11,2],
[5,8,4,5]
]
 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> all = new ArrayList<>();
helper(root, new ArrayList<Integer>(), all, , sum);
return all;
} private void helper(TreeNode root, List<Integer> list, List<List<Integer>> all, int currSum, int sum) {
// exit condition
if (root == null) return; list.add(root.val);
currSum += root.val; if (currSum == sum && root.left == null && root.right == null) {
all.add(new ArrayList<Integer>(list));
} helper(root.left, list, all, currSum, sum);
helper(root.right, list, all, currSum, sum);
list.remove(list.size() - );
}
}

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
 public class Solution {
public int pathSum(TreeNode root, int sum) {
int[] arr = new int[];
preOrder(root, sum, arr);
return arr[];
} public void preOrder(TreeNode root, int sum, int[] count) {
if (root == null) return;
printSums(root, sum, , count);
preOrder(root.left, sum, count);
preOrder(root.right, sum, count);
} public void printSums(TreeNode root, int sum, int currentSum, int[] count) {
if (root == null) return;
currentSum += root.val;
if (currentSum == sum) {
count[]++;
}
printSums(root.left, sum, currentSum, count);
printSums(root.right, sum, currentSum, count);
}
}

Path Sum I && II & III的更多相关文章

  1. [Leetcode][JAVA] Path Sum I && II

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  2. LeetCode:Path Sum I II

    LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...

  3. Path Sum I&&II

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

  4. leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...

  5. 1. Two Sum I & II & III

    1. Given an array of integers, return indices of the two numbers such that they add up to a specific ...

  6. Leetcode 39 40 216 Combination Sum I II III

    Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combin ...

  7. Two Sum I & II & III & IV

    Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...

  8. 【leetcode】Path Sum I & II(middle)

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

  9. combination sum(I, II, III, IV)

    II 简单dfs vector<vector<int>> combinationSum2(vector<int>& candidates, int targ ...

随机推荐

  1. loadrunner / socket level data and wininet level data

    s loadrunner录制脚本时,此网站证书无效/无法录制到脚本问题解决 http://www.51testing.com/html/64/497364-3569552.html lr录制后空白_l ...

  2. JAVA核心技术I---JAVA基础知识(单例模式和final关键字)

    一:单例模式 C++设计模式中提及,不再赘述设计模式---对象性能模式之单例模式(Singleton) public class single{ static single Instance=new ...

  3. swagger使用一新手篇

    本文转自:http://javatech.wang/index.php/archives/74/ 先简单介绍下项目环境: JDK1.7 Spring 3.2.2 swagger-springmvc 1 ...

  4. Linux下Maven私服Nexus3.x环境构建操作记录【转】

    Maven介绍Apache Maven是一个创新的软件项目管理和综合工具.Maven提供了一个基于项目对象模型(POM)文件的新概念来管理项目的构建,可以从一个中心资料片管理项目构建,报告和文件.Ma ...

  5. bzoj千题计划312:bzoj2119: 股市的预测(后缀数组+st表)

    https://www.lydsy.com/JudgeOnline/problem.php?id=2119 题意:将给定数组差分后,求ABA形式的字串个数,要求|B|=m,|A|>0 1.后缀数 ...

  6. ArcGis Python脚本——批量添加字段

    先看如何增加一个字段 函数:arcpy.AddField_management 语法:AddFields_management (in_table, field_description) 参数 说明 ...

  7. 细说java之编码

    JAVA编程中涉及的编码 1.J2EE中涉及的编码(1) HttpServletResponse响应内容编码: resp.setCharacterEncoding(charset); (2)JSP文件 ...

  8. Kettle系列:Pentaho DI (Kettle) 下载地址

    Kettle 8 已经发布, 下载地址还不太好找, 这里记录一下: 注: 所有大型软件升级都需要谨慎,  尤其是大版本的第一个小版本都不推荐在生产环境使用. github 总是有最新版 https:/ ...

  9. electron-vue项目搭建

    参考:https://simulatedgreg.gitbooks.io/electron-vue/content/cn/getting_started.html 1. cnpm install -g ...

  10. 让WeuiPicker隐藏日期中的日,只保留年月

    weui.datePicker({ start: 1990, end: new Date().getFullYear() + 3, defaultValue: [year, month, 1], on ...