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

需如下树节点求和

    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. 计算节点宕机了怎么办?- 每天5分钟玩转 OpenStack(43)

    Rebuild 可以恢复损坏的 instance. 那如果是宿主机坏了怎么办呢? 比如硬件故障或者断电造成整台计算节点无法工作,该节点上运行的 instance 如何恢复呢? 用 Shelve 或者 ...

  2. 每天一个linux命令目录

    出处:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每 ...

  3. openstack 命令行管理 - 目录

    原文http://blog.csdn.net/signmem/article/details/19513775 相关 openstack  命令行管理, 分下面部分进行介绍 openstack 命令行 ...

  4. 使用本地JConsole监控远程JVM(最权威的总结)

    问题背景   Tomcat经常崩溃crash,想看看JVM内存使用情况,就想到了用Jconsole监控,以前只是监控本地的JVM,这次要监控远程的,遇到了不少问题.   经过几个小时的努力,参考了众多 ...

  5. 使用开发者工具调试jsp页面中的脚本

    只举例火狐和谷歌.如果是火狐,一般是用firebug,首先确保开启脚本调试: 然后刷新一下要调试的页面,点击firebug中的行内,选择当前页面: js文件一般直接显示的是js文件的名字,而页面一般是 ...

  6. Struts与Struts2的区别

    Struts与Struts2的区别 首先看一张Struts2的发展路线图:       从Struts2的发展过程来看,Struts2继承了Struts与Webwork的特性,形成了新的框架.但是它的 ...

  7. WPF 开源Chart控件

    控件: Icon URL   Supplier Dynamic Data Display 2009

  8. Bootstrap 简介

    一.Bootstrap介绍 Bootstrap 是最受欢迎的 HTML.CSS 和 JS 框架,用于开发响应式布局.移动设备优先的 WEB 项目.本课时讲解 Bootstrap 的概念,并介绍 Boo ...

  9. POJ3281Dining[最大流]

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16352   Accepted: 7307 Descripti ...

  10. USACO2.4 The Tamworth Two[模拟]

    题目描述 两只牛逃跑到了森林里.农夫John开始用他的专家技术追捕这两头牛.你的任务是模拟他们的行为(牛和John). 追击在10x10的平面网格内进行.一个格子可以是: 一个障碍物, 两头牛(它们总 ...