leetcode — path-sum
/**
* 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的更多相关文章
- 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 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [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 ...
- [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 ...
- [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 ...
- LeetCode Path Sum IV
原题链接在这里:https://leetcode.com/problems/path-sum-iv/description/ 题目: If the depth of a tree is smaller ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- Oracle在.sql文件中创建存储过程
创建存储过程的语法网上到处都有. 可我执行了半天都创建不成功. 最后,发现! 在最后加个 / 就可以了!!! 真坑啊 今天连续被Oracle坑了两次了. 最后,感谢这个人https://blog.cs ...
- 数据分析——pandas
简介 import pandas as pd # 在数据挖掘前一个数据分析.筛选.清理的多功能工具 ''' pandas 可以读入excel.csv等文件:可以创建Series序列,DataFrame ...
- python学习:元组和嵌套
tuple(元组):只是可读,不可以修改# tup1 = () #空元组# tup2 = (20,) #元组内有一个元素,需要在元素后添加逗号 a = (1,2,3,4)print(a[1])a[1] ...
- linux 做了raid后,硬盘坏了更换问题
系统做完raid1后发现 raid盘坏了,硬盘都是热插拔的,更换后,需要简单配置一下才能自动进行镜像拷贝. 在pd mgmt 页面,选择新加入的硬盘,按F2,选择 make global HS选项 选 ...
- sqlserver 评估过期
解决:重新打开安装中心->维护-->版本升级 ,重新输入序列号 即可 sqlserver2008企业级序列号:JD8Y6-HQG69-P9H84-XDTPG-34MBB
- node05
1.ejs: const ejs = require('ejs') ejs.renderFile('./template/a.ejs', {name:'cc'}, function (err, dat ...
- Prometheus — Process-exporter进程监控
由于我们常用的node_exporter并不能覆盖所有监控项,这里我们使用Process-exporter 对进程进行监控. 安装process-exporter wget https://githu ...
- vue组件之间的传值方式
一.父组件向子组件传值方式 1.1父组件向子组件传数据方式 <!DOCTYPE html> <html lang="en"> <head> &l ...
- Python函数式编程之闭包
-------------------------函数式编程之*******闭包------------------------ Note: 一:简介 函数式编程不是程序必须要的,但是对于简化程序有很 ...
- LoadRunner(三)——LR相关概念&组成部分
参考学习感谢:<精通软件性能测试与LoadRunner实战> 一.运行机制和主要组成部分 1.LoadRunner主要由VuGen.Controller和Analysis三部分构成: 2. ...