[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 ...
随机推荐
- apache目录浏览
DocumentRoot "/Library/WebServer/Documents" <Directory "/Library/WebServer/Documen ...
- sdk添加新的C文件编译出错
make: *** 没有规则可以创建"test.elf"需要的目标"mike.c.o". 停止. 解决方法: 重新建立工程并且编译文件
- C++基础-01
指针 - 指针的基本操作间接引用指针所指向的对象 - 机器一般按字节寻址,所以能够独立分配的最小空间是1字节,也就是说指针指向的 最小空间为1字节.特别地,对于bool变量,虽然实际需要的是1bit, ...
- c语言选择排序
简单选择排序是经常用到的一种排序算法. 原理: 1.简单选择排序一句话概括:每次选择无序数列中最小的将其放在有序数列的最后. 2.在简单选择排序中,我们用初始化的数字int a[6]={2,5,6,3 ...
- UILabel添加图片之富文本的简单应用
若想对UILabel添加图片,那么就需要使用NSMutableAttributedString来定义先定义一个普通的label UILabel *lab = [[UILabel alloc]initW ...
- python学习之——pip的安装与使用
安装步骤: 前提:已经安装python 1.下载安装包并解压: easy_install 地址:https://pypi.python.org/pypi/setuptools#downloads pi ...
- 关于for循环删除数组内容出现的问题
昨天用for循环进行数组去重的时候出现的问题, 首先,用双重for循环把前一个和所有后面的元素进行比较,如果相等则删除. 但是,如果数组里面有三个以上连续相等的元素的时候,就会出现问题. var ar ...
- colorbox 自适应 高度
$(".example3").colorbox({ inline: true, scrolling: false , onComplete: ...
- Git之VS2010实践
对于我们经常在VS2010下编程的开发人员来说,强大的SCM工具Git貌似对我们很陌生.对于Git,我在我的另一篇博客<Git学习笔记>中已做过介绍,下面我再简单介绍一下Git在VS201 ...
- java虚拟机运行时乱码问题
问题: Android端通过socket发送文本到windows,windows调用系统剪切板进行粘贴的操作,java服务端在eclipse下直接运行粘贴的文本无乱码,打包jar后粘贴的文本乱码. 解 ...