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 ...
随机推荐
- Object的数据属性和访问器属性
一.数据属性 1.数据属性:它包含的是一个数据值的位置,在这可以对数据值进行读写. 2.数据属性包含四个特性,分别是: configurable:表示能否通过delete删除属性从而重新定义属性,能否 ...
- 剑指Offer_编程题_13
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. class So ...
- docker 基础之镜像加速
国内访问 Docker Hub 有时会遇到困难,此时可以配置镜像加速器 对于使用 systemd 的系统,用 systemctl enable docker 启用服务后,编辑 /etc/systemd ...
- 使用mysql存放Ambari元数据的配置案例
使用mysql存放Ambari元数据的配置案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.环境准备 详情请参考我之前的笔记:离线方式部署Ambari2.6.0.0 中关 ...
- Spring Boot笔记十:IOC控制反转
目录 IOC控制反转和DI依赖注入 IOC实现Hello World Spring IOC容器怎么知道哪些是管理的对象? IOC容器getBean方法的三种签名 xml配置文件的import导入 @A ...
- 16.观察者模式(Observer Pattern)
动机(Motivate): 在软件构建 过程中,我们需要为某些对象建立一种“通知依赖关系” --------一个对象(目标对象)的状态发生改变,所有的依赖对象(观察者对象)都将得到通知.如果这 ...
- mq【转】
kafka解决了什么问题? mq的几个指标 1.消息堆积能力.两亿条1k大小消息体的消息发上来,积压一周不消费,机器哭不哭. 2.吞吐量.来个峰值,每秒两万,连续两小时,临时扩容扛不扛得住. 3.安全 ...
- 29. SpringBoot Redis 非注解
1. 引入依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- ****** 二 ******、软设笔记【数据结构】-KMP算法、树、二叉树
五.KMP算法: *KMP算法是一种改进的字符串匹配算法. *KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与主串的匹配次数以达到快速匹配的目的.具体实现就是实现一个next()函 ...
- luogu 2216 理想的正方形 单调队列(其实没有DP)
#include<bits/stdc++.h> using namespace std; ; ; int a,b,n; int g[A][A],q[A][N],Q[A][N]; int h ...