一个二叉树,是否存在从根节点到叶子节点的路径,其节点的值的和为指定整数,如果有,打印出所有数组。

需如下树节点求和

    5
     /  \
   4     8
    /     /  \
  11  13    4
  / \       /  \
  7    2      5   1

JavaScript实现

window.onload = function() {
var n1 = new TreeNode(1, null, null),
n51 = new TreeNode(5, null, null),
n2 = new TreeNode(2, null, null),
n7 = new TreeNode(7, null, null),
n42 = new TreeNode(4, n51, n1),
n13 = new TreeNode(13, null, null),
n11 = new TreeNode(11, n7, n2),
n8 = new TreeNode(8, n13, n42),
n4 = new TreeNode(4, n11, null),
n5 = new TreeNode(5, n4, n8); var sum = 22; var res = getPathSum(n5, sum);
console.log('res: ', res); var has = hasPathSum(n5,22);
console.log('has: ', has); var count = pathCount(n5,22);
console.log('count: ', count);
} function TreeNode(val, left, right) {
this.val = val;
this.left = left;
this.right = right;
} //path sum i(https://leetcode-cn.com/problems/path-sum-i/)
function hasPathSum(root, sum) {
if (root == null) return false; sum -= root.val; if (sum == 0 && root.left == null && root.right == null) return true; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
} //path sum ii(https://leetcode-cn.com/problems/path-sum-ii/)
function getPathSum(root, sum) {
var res = [],path = [];
dfs(root, sum, path, res);
return res;
} function dfs(root,sum,path,res){
if(root == null) return; sum -= root.val;
path.push(root.val); if(sum == 0 && root.left == null && root.right == null){
res.push(copy(path));
//这句可以加,也可以不加, 加上,可以减少后面的两个dfs内部的null判断,因为此时root的left和right都为null
return path.pop();
} dfs(root.left,sum,path,res);
dfs(root.right,sum,path,res); path.pop();
} function copy(a){
return JSON.parse(JSON.stringify(a))
} //path sum iii(https://leetcode-cn.com/problems/path-sum-iii/)
function pathCount(root,sum){
return helper(root,sum,[],0);
} //思路就是,深度优先遍历所有节点,用path记录从根节点到该节点的路径
//由于只计算从上到下的节点和,所以从当前节点沿着path向上求和
//到合适的节点就计数,直至到根节点,当前节点为终点的所有路径计数完毕
function helper(root,sum,path,p){
if(root == null) return 0;
//记录当前节点的值
path[p] = root.val;
//path此时记录的是根节点到当前节点的路径上的所有节点
let temp = 0, n=0;
//p是当前节点的位置,从当前节点开始向根节点一路做加法
for(let i=p;i>=0;i--){
temp += path[i];
//当前节点加到某节点符合,就计数,由于节点值可能为0或负值,此处不能break,还需继续计算
if(temp == sum) n++;
}
//path虽然是引用传递,但是left和right用的是同一个索引p+1,所以path中的值会被覆盖
//path中的值始终是到当前节点的路径值,不需要拷贝数组,也不需要弹出已经访问的值
let left = helper(root.left,sum,path,p+1);
let right = helper(root.right,sum,path,p+1); return n + left + right;
}

Path Sum的更多相关文章

  1. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  2. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  3. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  4. [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  5. [LeetCode] Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  6. [LeetCode] Path Sum 二叉树的路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  7. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  8. [leetcode]Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  9. [leetcode]Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

随机推荐

  1. sql中毫秒数与格式化时间的转换

    使用MYSQL自带的函数FROM_UNIXTIME(unix_timestamp,format). 如: SELECT FROM_UNIXTIME(1461201575895/1000,"% ...

  2. UNIX文件的权限之“设置用户ID位”

    用stat函数可以获取一个文件的状态信息,原型是这样的: int stat(const char *path, struct stat *buf); 其中结构体stat的结构: struct stat ...

  3. Hibernate双向多对多关联

    一.配置双向多对多关联 以Project类(项目)和Emp类(员工)为例: 1.创建Project类,并需要定义集合类型的Emp属性 public class Project { //编号 priva ...

  4. 第14章 集合框架(1)-List集合的各种类

    1.概述 1.1.Java集合框架的由来 1.2.什么是集合框架? 1.3.为什么需要集合框架 1.4.常用的框架接口规范 2.Vector类 2.1.存储原理 2.2.构造方法 2.3.常用方法 3 ...

  5. DbContext 和ObjectContext两者的区别

    http://blog.csdn.net/lvjin110/article/details/24642911 ObjectContext是一种模型优先的开发模式,DbContext是代码优先的开发模式 ...

  6. Linux下快速迁移海量文件的操作记录

    有这么一种迁移海量文件的运维场景:由于现有网站服务器配置不够,需要做网站迁移(就是迁移到另一台高配置服务器上跑着),站点目录下有海量的小文件,大概100G左右,图片文件居多.目测直接拷贝过去的话,要好 ...

  7. fMRI: spatial smoothing

    Source: Brain voyager support Theoretical Background Spatial smoothing means that data points are av ...

  8. c语言游戏推箱子

    前两天做了推箱子小游戏,看似简单的一个小游戏背后却 有巨大的秘密,这秘密就是一大堆逻辑. 自从学习了函数过后,的确是解决了很多问题,而且调用很方便,尽管我现在都不是很会调用. 写完一个函数,准备测试一 ...

  9. node基础10:处理异常

    1.处理异常 当发生异常时,如果不作处理,那么服务器会奔溃.由于node的异步调用的特性,所以不但要考虑主程序的异常,还有处理异步调用的异常. 代码如下: /** * server.js */ var ...

  10. jenkins 入门教程(中)

    接上回继续,本文以我托管在bitbucket上的一个开源项目spring-boot-rest-framework做为演示,讲解如何创建自动化部署. 一.创建item 点击ok继续,item的详情页面很 ...