[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 adding up all the values along the path equals the given sum.
For 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.
分治法可以解决,传入下层的sum为原来的sum-root.val即可
public boolean hasPathSum(TreeNode root, int sum) {
if(root==null)
return false;
if(root.left==null && root.right==null)
return root.val==sum;
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]
]
II就是要返回所有可能的path. 可以用分治法的思想去实现(把根节点加到左子树得到的list和右子树得到的list的第一位),不过较慢,因为要结果返回给上层。用单纯的dfs回溯也能很好地实现,而且较快。
分治法:
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> re = new ArrayList<List<Integer>>();
if(root==null)
return re;
if(root.left==null && root.right==null && root.val==sum) {
List<Integer> temp = new ArrayList<Integer>();
temp.add(root.val);
re.add(temp);
return re;
}
List<List<Integer>> left = pathSum(root.left, sum-root.val);
List<List<Integer>> right = pathSum(root.right, sum-root.val);
if(left.size()>0)
for(int i=0;i<left.size();i++) {
left.get(i).add(0,root.val);
re.add(left.get(i));
}
if(right.size()>0)
for(int i=0;i<right.size();i++) {
right.get(i).add(0,root.val);
re.add(right.get(i));
}
return re;
}
dfs:
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> re = new ArrayList<List<Integer>>();
if(root==null)
return re;
List<Integer> path = new ArrayList<Integer>();
collect(re, path, root, sum);
return re;
} public void collect(List<List<Integer>> re, List<Integer> path, TreeNode rt, int v) {
if(rt.left==null && rt.right==null && rt.val==v) {
List<Integer> temp = new ArrayList<Integer>(path);
temp.add(rt.val);
re.add(temp);
return;
}
path.add(rt.val);
if(rt.left!=null)
collect(re,path,rt.left,v-rt.val);
if(rt.right!=null)
collect(re, path, rt.right, v-rt.val);
path.remove(path.size()-1);
}
[Leetcode][JAVA] Path Sum I && II的更多相关文章
- 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 ...
- 【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 ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 437. Path Sum III_ Easy tag: DFS
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 666. Path Sum IV 二叉树的路径和 IV
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)
LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...
- 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 ...
随机推荐
- dg
package excel; import java.util.Scanner; public class doExcel { public static void main(String args[ ...
- 如何取消MSSQL自带智能提示步骤,使用第三方智能提示插件
步骤1如下: [工具]——[选项]——[文本编辑器]——[Transact-SQL]——[IntelliSense]——[Transact-SQL IntelliSense 设置]——(取消选择)—— ...
- 第九周PSP
工作周期:11.10-11.17 本周PSP: C类型 C内容 S开始时间 ST结束时间 I中断时间 T净时间(分) 文档 写随笔(PSP) 19:00min 22:00min 30min 90mi ...
- windows无法停用“Android Composite Interface”设备的解决方法
遇到这个问题时,解决方法如下: 打开设备管理器 -> 进程 -> 找到 adb.exe -> 右击选择结束进程 -> 然后重新弹出Android设备
- php版本引起的const问题
刚刚遇到一个问题,类中定义了一个常量: const USER = ['aa', 'bb', 'cc']; 在类中的静态函数中调用以上常量不会出错.网站中所有的网页均能正常打开. 而当push到线上后, ...
- [z]Oracle性能优化-读懂执行计划
http://blog.csdn.net/lifetragedy/article/details/51320192 Oracle的执行计划 得到执行计划的方式 Autotrace例子 ...
- Eclipse 在ubuntu桌面显示快捷启动以及解决Eclipse 在ubuntu中点击菜单栏不起作用的原因.
要在Eclipse中设置好之后,可以通过如下方式在周末显示快捷启动以及解决Eclipse在ubuntu高版本中点击菜单栏项不显示列表的问题 在usr/share/app-install/desktop ...
- ng2收获
1.devDependencies下只有在开发应用时才用得到这个我是知道的. 但是我不知道的事要想达到这个效果是要在生产环境安装包的时候必须要加个这个才行"--production" ...
- PHP array_multisort—对多个数组或多维数组进行排序
PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以 ...
- postgresql+slony-i安装配置主从
slon软件下载地址:slony1-1.2.6 http://slony.info/downloads/1.2/source/ postgresql下载地址: http://www.postgresq ...