112. Path Sum

Easy

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.

Note: A leaf is a node with no children.

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.

package leetcode.easy;

/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class PathSum {
public boolean hasPathSum(TreeNode root, int sum) {
if (null == root) {
return false;
} else if (null == root.left && null == root.right && 0 == sum - root.val) {
return true;
} else {
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
} @org.junit.Test
public void test() {
int sum = 22;
TreeNode tn11 = new TreeNode(5);
TreeNode tn21 = new TreeNode(4);
TreeNode tn22 = new TreeNode(8);
TreeNode tn31 = new TreeNode(11);
TreeNode tn33 = new TreeNode(13);
TreeNode tn34 = new TreeNode(4);
TreeNode tn41 = new TreeNode(7);
TreeNode tn42 = new TreeNode(2);
TreeNode tn46 = new TreeNode(1);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = tn31;
tn21.right = null;
tn22.left = tn33;
tn22.right = tn34;
tn31.left = tn41;
tn31.right = tn42;
tn33.left = null;
tn33.right = null;
tn34.left = null;
tn34.right = tn46;
tn41.left = null;
tn41.right = null;
tn42.left = null;
tn42.right = null;
tn46.left = null;
tn46.right = null;
System.out.println(hasPathSum(tn11, sum));
}
}

LeetCode_112. 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. Path Sum

    需如下树节点求和 5  /  \ 4     8  /     /  \ 11  13    4 / \     /  \  7    2      5   1 JavaScript实现 window ...

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

随机推荐

  1. tslint.json的配置项说明

    tslint.json的配置项说明   extends: 内设配置项名称 rules: 规则 { //ts专用 adjacent-overload-signatures : true, // Enfo ...

  2. html中的table导出Excel (亲测有用(●'◡'●))

    演示地址: http://www.jq22.com/yanshi3312 具体代码: <!DOCTYPE html> <html lang="zh-CN"> ...

  3. Maven Module和Maven Project的区别

    1.maven project和module相当于父子关系.2.当新建的项目中不存在父子关系时使用project.3.当项目中存在父子关系时用project做父工程,module做子工程,module ...

  4. html5文件夹上传源码

    前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...

  5. leetcode解题报告(21):Majority Element

    描述 Given an array of size n, find the majority element. The majority element is the element that app ...

  6. 【优化算法】变邻域搜索算法(VNS)求解TSP(附C++详细代码及注释)

    00 前言 上次变邻域搜索的推文发出来以后,看过的小伙伴纷纷叫好.小编大受鼓舞,连夜赶工,总算是完成了手头上的一份关于变邻域搜索算法解TSP问题的代码.今天,就在此给大家双手奉上啦,希望大家能ENJO ...

  7. gdb 预备知识

    1.gcc的-g选项 如果要使用gdb进行调试,必须在编译时在gcc中加入-g选项,使用参数 -g 表示将源代码调试信息编译到可执行文件中. #include <stdio.h> int ...

  8. ZROI Day6比赛总结

    比赛还没结束而且我没有参加比赛就来这里了. T1 略 T2 设\(ans_d\)表示\(d|b_i\)的方案数(最后反演一下就可以) 设\(d\not|a_i\)的个数为\(l\)(可以\(O(n\l ...

  9. 利用 Python 尝试采用面向对象的设计方法计算图形面积及周长

    利用 Python 尝试采用面向对象的设计方法.(1)设计一个基类 Shape:包含两个成员函数:def cal_area(): 计算并返回该图形的面积,保留两位小数:def cal_perimete ...

  10. mac 启动mysql

    sudo /usr/local/mysql/support-files/mysql.server stop sudo /usr/local/mysql/support-files/mysql.serv ...