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 ... 
随机推荐
- sqlzoo:2
			顯示具有至少2億人口的國家名稱. 2億是200000000,有八個零. SELECT name FROM world 找出有至少200百萬(2億)人口的國家名稱,及人均國內生產總值. select n ... 
- 原生js的联动全选
			开发应用中有很多工具可以使用,下面介绍一个原生js写的联动全选思路!!! <!DOCTYPE html> <html lang="en"> <head ... 
- 解决挂载nfs共享目录失败的问题
			现象:在192.168.82.131上 启动了nfs服务,并共享了/nfsfile目录,在另一台主机(ip: 192.1168.82.115)挂载的时候一直阻塞 1 初步分析是防火墙拦截导致,于是进行 ... 
- 用万能马甲免费看VIP电影
			什么是:万能马甲? 它浏览器的一个扩展程序插件,安装后能看一些热门网址的VIP特权,免费观看付费电影等. 使用过程: 1. 访问网址:http://www.wndd123.com/ 点击免费看电影时提 ... 
- vue项目开发时怎么解决跨域
			vue项目中,前端与后台进行数据请求或者提交的时候,如果后台没有设置跨域,前端本地调试代码的时候就会报“No 'Access-Control-Allow-Origin' header is prese ... 
- 【原创】XAF ITreeNode+NonPersistent  使用方式
			在XAF中使用非持久化对象创建出TreeList这种树形结构 private void SetShowRFID(TArchivesBorrow archivesInStorage, string rf ... 
- 封装一个 员工类 使用preparedStatement 查询数据 (1)
			创建员工类 自动生成get set 方法 package cn.hph; public class emp { //定义表中的属性 private int id; private String en ... 
- 简单工厂模式demo
			1. 简单工厂模式 domain的接口 public interface Color{ public void display(); } red public Class Red implements ... 
- MyEclipse最新版-版本更新说明及下载 - MyEclipse官方中文网
			http://www.myeclipsecn.com/learningcenter/myeclipse-update/ [重要更新]MyEclipse 2015正式版发布 [重要更新]MyEclips ... 
- 整理4种Vue组件通信方式
			整理4种Vue组件通信方式 重点是梳理了前两个,父子组件通信和eventBus通信,我觉得Vue文档里的说明还是有一些简易,我自己第一遍是没看明白. 父子组件的通信 非父子组件的eventBus通信 ... 
