/**
* Source : https://oj.leetcode.com/problems/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.
*/
public class PathSum { public boolean exists (TreeNode root, int target) {
return hasSum(root, target, 0);
} public boolean hasSum (TreeNode root, int target, int sum) {
if (root == null) {
if (target == sum) {
return true;
}
return false;
}
boolean result = hasSum(root.leftChild, target, sum + root.value);
if (result) {
return true;
}
result = hasSum(root.rightChild, target, sum + root.value);
if (result) {
return true;
}
return false;
} public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
} private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value; public TreeNode(int value) {
this.value = value;
} public TreeNode() { }
} public static void main(String[] args) {
PathSum pathSum = new PathSum();
char[] arr0 = new char[]{'#'};
char[] arr1 = new char[]{'3','9','2','#','#','1','7'};
char[] arr2 = new char[]{'3','9','2','1','6','1','7','5'}; System.out.println(pathSum.exists(pathSum.createTree(arr0), 0));
System.out.println(pathSum.exists(pathSum.createTree(arr1), 5));
System.out.println(pathSum.exists(pathSum.createTree(arr2), 13));
} }

leetcode — path-sum的更多相关文章

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

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

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

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

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

  5. [LeetCode] Path Sum IV 二叉树的路径和之四

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  6. LeetCode Path Sum IV

    原题链接在这里:https://leetcode.com/problems/path-sum-iv/description/ 题目: If the depth of a tree is smaller ...

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

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

  9. LeetCode: Path Sum 解题报告

    Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

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

随机推荐

  1. BZOJ5304 : [Haoi2018]字串覆盖

    离线处理所有询问. 对于$r-l\leq 50$的情况: 按照串长从$1$到$51$分别把所有子串按照第一位字符为第一关键字,上一次排序结果为第二关键字进行$O(n)$基数排序. 同理也可以用上一次比 ...

  2. [转].Net-C#的委托(代理)和事件

    一.代理 首先我们要弄清代理是个什么东西.别让一串翻译过来的概念把大家搞晕了头.有的文章把代理称委托.代表等,其实它们是一个东西,英文表述都是“Delegate”.由于没有一本权威的书来规范这个概念, ...

  3. section标签实现文字滚动

    h5新增样式 section标签 使用demo //h5新增属性 h5新增滚动的标签 <marquee> <div style="padding-left: 20px;ma ...

  4. c#坐标系互相转换

    转自群友的博客:https://www.xiaofengyu.com/?p=108 群友的github地址:https://github.com/jfwangncs/GPSConvert  各种坐标系 ...

  5. ES6学习

    一.ES6的特点 1.let(变量),const(常量) 2.在ES6中不能重复定义 3.块级作用域 普通作用域 if(true){ var test =1; } console.log(test); ...

  6. Mac_mac下使用ll等指令

    Linux有ll等命令,切换到MAC下这些指令是没有的 其实就是别名,用ls -alF也能做到 添加方法: vim ~/.bash_profile 输入一下内容 alias ll='ls -alF'a ...

  7. 获取jwt(json web token)中存储的用户信息

    一个JWT实际上就是一个字符串,它由三部分组成,头部(header).载荷(Payload)与签名. Payload payload中可以保存用户的信息. var claims = new Claim ...

  8. Openstack的视频学习

    0.安装环境准备 部署架构: 网络模式(红色Net0为管理网络,Net1接外网,Net2是接虚拟机网络流量的): 虚拟化平台为VirtualBox,虚拟网络Host-Only网络的配置: Net0:管 ...

  9. java代码的编译、执行过程

    Java代码编译是由Java源码编译器来完成,流程图如下所示: Java字节码的执行是由JVM执行引擎来完成,流程图如下所示: Java代码编译和执行的整个过程包含了以下三个重要的机制: Java源码 ...

  10. 使用SSM重新开发计科院网站

    一.游览 在游览器地址栏输入:http://localhost:8080/index,即访问计科院首页,由于前期对数据库以及JavaBean的设计考虑不够充分,导致后期的代码臃肿,所以项目启动时对首页 ...