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 ...
随机推荐
- Eigen
Eigen: https://eigen.tuxfamily.org/dox/GettingStarted.html
- 前端学习笔记--CSS布局--盒子模型
1.概述: 2.盒子模型 overflow属性: border属性: 1.padding属性: margin:
- python 单引号与双引号的转义
import simplejson a = """{"a":"\\""}""" b = & ...
- win32窗口程序分析
1.分析消息的附加参数 例如:为了查看程序处理了哪些消息 在回调函数中调用输出函数,在控制台中输出消息的值:
- Java Part 001( 03_01_数据类型和运算符 )
注释 Java语言的注释一共有三种类型,分别是单行注释.多行注释和文档注释. 1. 单行注释 单行注释就是在程序中注释一行代码,在Java语言中,使用双斜线“//”进行单行注释. 2. 多行注释 多行 ...
- LOJ P10151 分离与合体 题解
Analysis 区间dp+记录路径 用dfs倒着找倒数第几次合并 #include<iostream> #include<cstdio> #include<cstrin ...
- Postgresql 解决锁表
转载地址:https://blog.csdn.net/cicon/article/details/68068462##一.postgresql解决锁表--查询是否锁表了select oid from ...
- TensorFlow(八):tensorboard可视化
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from tensorflow.c ...
- linux系列(五):rm命令
rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根目录)下执行rm * -rf).所以,我们在执行rm之前最好先确认一下在哪个目录,到底要删除什么东西 ...
- IDEA2018全局搜索中搜索jar包/lib
搜索jar包 配置find in path ctrl+shift+f 点击 ...处 把lib添加进来 点击OK保存 之后在IDEA中 , 双击shift , 调出全局搜索框就可以搜索到 jar包里的 ...