[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 ...
随机推荐
- 在JSP中使用JavaBean
//创建一个PersonBean类 public class PersonBean { private String name; private int age; public Pe ...
- Oracle中的sql操作符 和分析函数
Oracle中的操作符算术操作符:无论是在sqlserver,或者是java中,每种语言它都有算术操作符,大同小异. Oracle中算术操作符(+)(-)(*)(/) 值得注意的是:/ 在oracle ...
- Html登录表单阻止自动填充
设置属性 autocomplete="off" 阻止浏览器从cache获取数据填充登录表单. <input type="text" name=" ...
- VLC嵌入网页,终于要成功了!
<OBJECT classid="clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921" width="640" heig ...
- js的form基础知识点
在HTML 中,表单是由<form>元素来表示的,而在JavaScript 中,表单对应的则是HTMLForm-Element 类型.HTMLFormElement 继承了HTMLElem ...
- Java动手动脑(二)
1>类的对象实例化 由于main为静态类型,所以在调用函数时也必须调用静态方法,如上代码中的求平方数的静态方法,如何在静态main中调用非静态类的方法呢? 静态方法只能直接访问静态成员,无法访问 ...
- 安装CocoaPods碰到的问题
1.安装完Pods后第一次使用pod install命令提示"Setting up CocoaPods master repo" 解决办法: 第一次使用pod命令时,先执行以下po ...
- 我利用网上代码开发的JQuery图片插件
我利用网上代码开发的JQuery图片插件 代码如下 (function($){ $.fn.FocusPic = function(options){ var defaults = { interval ...
- [翻译][erlang]cowboy路由模块使用
Cowboy是基于Erlang实现的一个轻量级.快速.模块化的http web服务器. 本文官方原文:http://ninenines.eu/docs/en/cowboy/1.0/guide/rout ...
- 在vCenter5.5中为用户创建角色,管理虚拟机
在vSphere的使用中,如有只有vCenter+ESXi节点的两级配置,为了达到多租户管理及权限分配,可以在vCenter5.5中为用户创建角色,管理虚拟机 1.以管理员身份登陆vCenter 2. ...