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. 跨站请求伪造(CSRF 或者 XSRF)与跨站脚本(XSS)

    跨站请求伪造 跨站请求伪造(英语:Cross-site request forgery),也被称为 one-click attack 或者 session riding,通常缩写为 CSRF 或者 X ...

  2. archlinux 使用 postgresql

    一.安装与初始化 1.初始化数据目录 默认安装后已创建 postgres 系统用户 切换到 postgres 用户 $ sudo -iu postgres # Or su - postgres for ...

  3. 使用docker简单启动springboot项目

    1.搭建docker环境 需要linux系统必须是centOS7以上 执行一下命令: yum install epel-release –y yum clean all yum list 2.安装 y ...

  4. TDOA 之数据测试

    许久没有更新TDOA了,近期断断续续编写学习,开始测试TDOA数据.记录如下 1 测试场地,如下所示,4个基站摆放位置是一个正方形,变成为1.6m,被测试标签放置正中心. 2 获得原始数据 test_ ...

  5. 使用JSP/Servlet技术开发新闻发布系统---JSP数据交互(二)

    JSP内置对象application application对象 JSP常用的内置对象 对象的作用域 作用的分类 对象的作用域 page作用域 实例 //页面1 <% String name = ...

  6. html表格按长度换行

    <table style="table-layout:fixed; WORD-BREAK: break-all; WORD-WRAP: break-word; width:200px; ...

  7. MongoDB-3.2 oplog删除策略优化

    MongoDB oplog是一个capped collection,创建capped collection时,createCollection可以设置size(最大字节数)和max(最大文档数)的参数 ...

  8. Linux core dump 诊断进程奔溃退出

           最近项目中出现了一个问题,服务器端程序会突然崩溃退出,我们采取了coredump技术以找到崩溃原因,即确定进程退出时正在执行的函数是哪个,其状态如何.       如果系统开启了core ...

  9. java application指的到底是什么?

    在Java语言中,能够独立运行的程序称为Java应用程序(Application).Java语言还有另外一种程序——Applet程序.Applet程序(也称Java小程序)是运行于各种网页文件中,用于 ...

  10. exam9.3

    #   用  户  名  公园 计划 抽卡   总分  19 859乔屹 100 03:15:05 40 03:14:01   140 03:15:05 emm 怎么讲 T2我把自己优化掉了40分 优 ...