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的更多相关文章

  1. leetcode -day17 Path Sum I II &amp; Flatten Binary Tree to Linked List &amp; 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 ...

  2. 【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 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  9. 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 ...

随机推荐

  1. selenium—八种定位方法

    find_element_by_id() find_element_by_name() find_element_by_class_name() find_element_by_tag_name() ...

  2. Linux(Centos) 安装windows字体

    有时候在Linux中需要用到windows字体,比如微软雅黑字体,这个时候,可能就需要我们手动去安装字体了(当然,如果服务器上没装过),简单几步如下: 1.在$WINDOWS/Fonts目录中找到对应 ...

  3. Linux下错误的捕获:全局变量errno和strerror()

    经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因.这个时候使用 ...

  4. 利用 PortableBasemapServer 发布地图服务

    前段时间需要给自己的C/S系统加一个地图,但是没有数据,于是就想到了使用网上的切片地图,但是C/S系统又不能联网,于是就想本地发布切片服务来使用. 本来想用ArcGIS Server来发布从网上下载的 ...

  5. MY SQL8.0里程碑发布

    MySQL 开发团队于 12 日宣布 MySQL 8.0.0 开发里程碑版本(DMR)发布! 可能有人会惊奇 MySQL 为何从 5.x 一下跳跃到了 8.0.事实上,MySQL 5.x 系列已经延续 ...

  6. Symmetric Difference

    function sym(args) { //return args; var arr = []; for(var i = 0; i < arguments.length; i++){ arr. ...

  7. c语言插入排序

    对于小规模输入,插入排序是一种非常快速的排序算法,且原理简单,结构紧凑. 插入排序的原理:从序列中第二个数A开始,将A,插入前面已经排好的序列中,形成一个新的排序好的序列,以此类推到最后一个元素. 参 ...

  8. .net学习笔记--序列化与反序列化

    序列化其实就是将一个对象的所有相关的数据保存为一个二进制文件(注意:是一个对象) 而且与这个对象相关的所有类型都必须是可序列化的所以要在相关类中加上 [Serializable]特性 对象类型包括:对 ...

  9. js原生ajax请求get post笔记

    开拓新领域,贵在记录.下面记录了使用ajax请求的get.post示例代码 //ajax get 请求获取数据支持同步异步 var ajaxGet = function (reqUrl, params ...

  10. Sql Server 日期查询

    当前月: USE [DBName] Go Use Database, Declare Variables DECLARE @ReportGenerationDate DATE DECLARE @Rep ...