LeetCode_112. Path Sum
112. 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.
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的更多相关文章
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- [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] 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. ...
- [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 ...
- 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 ...
- Path Sum
需如下树节点求和 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 JavaScript实现 window ...
- [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 ...
随机推荐
- WebAPI ModelValidata(模型验证)——DataAnnotations 解析
爱做一个新的项目,在该项目中的 WebAPI 中对数据的验证用到了 ModelValidata, 以前也没有用到过,也不是很熟悉,在查看了一些资料和代码后稍有了解,这里记录下来. 这里主要介绍的是 S ...
- Eigen
Eigen: https://eigen.tuxfamily.org/dox/GettingStarted.html
- Codeforces Round #609 (Div. 2) D. Domino for Young
链接: https://codeforces.com/contest/1269/problem/D 题意: You are given a Young diagram. Given diagram i ...
- UML再论关系extend和include
我在画用例图时,图中既有extend关系也有include关系,师父就问我这两种关系的区别,我在画的时候确实查阅了很多资料,可是在问的时候还是回答不上来,这就是这篇博客得来的缘由了. [include ...
- js事件冒泡/捕获
- BZOJ 1181: [CROATIAN2009] IZBROI选举(二分+dp)
题面 在一个地区的选举中,共有V个人参加了投票,每一票只可能投给N个政党中的一个.当地的议会共有M个席位.不妨将N个政党编号为1到N,并且设编号为i的政党最终的得票为Vi,则议会中的席位按如下规则分配 ...
- [Javascript] Construct a Regex to Match Twitter Mentions with Regexr
regexr is a powerful tool for debugging existing regexes and creating new ones. In this lesson, we'l ...
- Hive的自定义函数
功能: 通过人的生日,算出人的生肖和星座. 先在hive中创建一个表: 往这表中导入数据: 导入的数据为: 可以成功查询: 编写自定义函数代码:如下 package cn.tendency.wenzh ...
- 22 | MySQL有哪些“饮鸩止渴”提高性能的方法?
不知道你在实际运维过程中有没有碰到这样的情景:业务高峰期,生产环境的MySQL压力太大,没法正常响应,需要短期内.临时性地提升一些性能. 我以前做业务护航的时候,就偶尔会碰上这种场景.用户的开发负责人 ...
- 坑爹的IE8
1.不能用trim(),要用$.trim() var aa = $("#id").val().trim() 这样素不行的,要变成这样Jquery的方式 var aa = $ ...