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. python自动化开发-[第十二天]-前端Css

    CSS基本语法 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. selector { property: value; property: value; ... property: v ...

  2. jmeter5.0 while controller使用总结

    while controller 配合sql使用的方式 在while控制器条件中填空,这样当里面的请求断言失败后就会跳出循环 在while控制器条件中填LAST,当里面的请求断言失败后就会跳出循环,如 ...

  3. 2017-12-15python全栈9期第二天第七节之练习题

    #!/user/bin/python# -*- coding:utf-8 -*-print(6 or 2 > 1)print(3 or 2 >1 )print(0 or 5 <4)p ...

  4. 2017-12-14python全栈9期第一天第四节之python分类

    python的环境. 编译型:一次性将所有程序编译成二进制文件. 缺点:开发效率低,不能跨平台. 优点:运行速度快. :C,C++等等. 解释型:当程序执行时,一行一行的解释. 优点:开发效率高,可以 ...

  5. ntp 时间同步

    NTP 是网络时间协议(Network Time Protocol)的简称,通过 udp 123 端口进行网络时钟同步 一.安装 # 既可做服务端也可做客户端 yum install -y ntp # ...

  6. python 内置函数,匿名函数,sorted,filter,map,递归,二分法,冒泡算法 eval

    ############################总结#################################1. lambda 匿名函数 语法——lambda 参数:返回值 __na ...

  7. 原生JavaScript运动功能系列(二):缓冲运动

    匀速运动实现回顾 缓冲运动剖析 示例实现 方法提取 匀速运动实现回顾及缓冲运动剖析: 在这个系列的上一篇博客中原生JavaScript运动功能系列(一):运动功能剖析与匀速运动实现就运动的核心功能组成 ...

  8. Vector集合

    Vector集合也是List接口的一个实现类,但是它是同步的,这就意味着是单线程的,速度比较慢,被ArrayList集合所取代了(PS:为什么我现在也还不知道,先记录了)

  9. C# string 保留数字英文字母

    using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressi ...

  10. Uncaught TypeError: $(…).orgcharts is not a function

    调整js顺序没有解决,最后增加NoConflict解决,注意红色部分 function initorgcharts() { var $jq = jQuery.noConflict(true); org ...