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. elasticsearch 集成springboot

    和jpa类似,很简单,很强大. pom <dependencies> <dependency> <groupId>org.springframework.boot& ...

  2. scrapy中的xpath用法和css的用法

    css 不包含那个类 response.css(".list-left dd:not(.page)") 获取属性和文本 img.css("a::text").e ...

  3. 阿里RocketMq(TCP模式)

    针对公司业务逻辑,向阿里云MQ发送指定数据,消费端根据数据来做具体的业务,分两个项目,一个生产端(Producer).一个消费端(Consumer) 生产端通过定时任务执行sql向阿里云MQ发送数据, ...

  4. java-map复合类型(HashMap-TreeMap)常用操作例子(适合初学者)

    package com.net.xinfang.reflect; import java.util.ArrayList; import java.util.HashMap; import java.u ...

  5. 原生JavaScript运动功能系列(三):多物体多值运动

    多物体同时出发运动函数实现 多属性同步运动变化实现 一.多物同时触发运动函数实现 前面两个动画示例基本理解了动画的核心:位置变化和速度变化,操作的核心就是定时器分段叠加属性值.但是动画还是基于单个元素 ...

  6. IO流--序列化流与反序列化流

    IO流--序列化流与反序列化流: 序列化流:把对象当做流一样写入到文本文件中 ObjectOutputSream(); 反序列化流:把文本文件中的流对象还原成对象ObjectInputSream(): ...

  7. ArcGis Python脚本——批量删除字段

    注意,ArcGis中shp属性表至少要保留除OID(FID).Shape之外的1个字段. 例:批量删除“省级行政区域”除“FID”.“Shape”.“Area”之外的字段. import arcpy ...

  8. git多人协作式开发时分支管理策略

    什么是 git-flow? Git Flow是一套使用Git进行源代码管理时的一套行为规范 主分支Master 首先,代码库应该有一个.且仅有一个主分支.所有提供给用户使用的正式版本,都在这个主分支上 ...

  9. 指定so动态链接库连接器

    在学习x86_64汇编时, 发现一旦使用glibc库函数, 如printf时, 一般是需要使用为ld传递命令行参数-lc来动态连接libc.so的, 但是, 生成的可执行文件却无法运行: 气煞我也! ...

  10. DosBox 的 DOSBOX.CONF 的详细配置说

    1.首先下载 DOSbox 0.72 版.   2.下载完毕,开始安装.安装到任意目录均可.安装完毕会在开始菜单生成程序组,DOSBox.conf 文件是 DOSbox 的配置文件,保持默认配置就可 ...