Path Sum I && II & III
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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【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 ...
- combination sum(I, II, III, IV)
II 简单dfs vector<vector<int>> combinationSum2(vector<int>& candidates, int targ ...
随机推荐
- Linux记录-在线扩容8e
1.fdisk -l 2.增加分区 3.3:键入 p,主分区,并键入3(编号): 默认起始扇区和结束扇区即可(键入两次Enter) 键入t,修改分区类型为8e: 键入w,写分区表,然后重启: 卷扩容, ...
- SQL Server 备份到网络盘网络映射盘
declare @DBName nvarchar(max) declare @BakName nvarchar(max) --在这里修改数据库名称 select @DBName='[LFBMP.PO] ...
- git的那些事
前言:记得在想学习git的时候,一直停留在思想的层面,总没有弄清楚它的运行机制,经常与github混淆,还好找到了一个好的教程,带我领略了git的风采 (一)git的优点 git的优点:版本控制在本地 ...
- Python复习笔记(七)线程和进程
1. 多任务 并行:真的多任务 并发:假的多任务 2. 多任务-线程 Python的 Thread模块是比较底层的模块,Python的 Threading模块 是对Thread做了一些包装,可以更加方 ...
- 缓存之 -Redis
其实这类服务还一样, server , client 两端... WIN 和 linux 均可,开源发现源码还是 C 看来 C 还是王者哦...后悔没深入学 Redis支持五种数据类型:string( ...
- maven项目的聚合与继承
maven项目的聚合与继承: 一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 1 <modules> 2 <module>模 ...
- 02-Unity深入浅出(二)
一. Unity声明周期 Unity容器为我们提供了6种生命周期,便于我们根据项目需求来选择使用. (1). 瞬时.默认省略即为瞬时,无论单线程还是多线程,每次都重新创建对象.new Transien ...
- 031、none和host网络的适用场景(2019-02-18 周一)
参考https://www.cnblogs.com/CloudMan6/p/7053617.html 本节开始,会学习docker的几种原生网络,以及如何创建自定义网络.然后探究容器之间如何通信, ...
- SpringBoot系列: Java应用程序传参和SpringBoot参数文件
===========================向java 程序传参的几种形式:===========================1. 使用 OS 环境变量. 这个不推荐. 2. 使用JVM ...
- Lua 函数链功能
函数链 http://lua-users.org/wiki/FiltersSourcesAndSinks A chain is a function that combines the effect ...