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 ...
随机推荐
- 前端学习笔记--html入门
1.什么是html 2.标签和元素: 标签可嵌套,要注意缩进 html文档中的元素分为:内容文本.标签 3.标签和属性: 4.html的文件结构: 5.标签 a标签: 还不知道跳转到哪里,可以使用虚拟 ...
- AfxBeginThread深入解析
看过<深入浅出MFC>书中,j.j.hou提到在创建新的线程时,最好不要直接使用CreateThread,虽然AfxBeginThread也是对CreateThread的封装,但是AfxB ...
- HTML怎么块外横向剧中
HTML 块外横向剧中 在HTML中有一个块外横向剧中的代码 那就是margin:0 auto 这个能是块内元素横向剧中 剧中前: 剧中后
- jQuery相关方法2
一.元素样式设置的方式(css,json键值对,链式编程) <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js ...
- TensorFlow(八):tensorboard可视化
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from tensorflow.c ...
- git 忽略文件 目录
git status 这里面的iml文件类似 eclipse .project文件 ,不能删除 .删除就不能识别项目了. 通过git .gitignore文件 过滤 git status gitig ...
- 数据结构实验之图论九:最小生成树 (SDUT 2144)
#include<bits/stdc++.h> using namespace std; typedef long long ll; struct node { int s, e; int ...
- P5346 【XR-1】柯南家族(后缀数组+主席树)
题目 P5346 [XR-1]柯南家族 做法 聪明性是具有传递性的,且排列是固定的 那么先预处理出每个点的名次,用主席树维护\(k\)大值 一眼平衡树,遍历的同时插入\(O(log^2n)\),总时间 ...
- HDU 6129 Just do it ——(找规律)
思路见:http://blog.csdn.net/qq_32506797/article/details/77206167. 利用二进制讲m次转化成log次然后进行转移. 代码如下: #include ...
- [CMS漏洞]EmpireCMS_V7.5的一次审计【转载】
i春秋作家:Qclover 原文来自:EmpireCMS_V7.5的一次审计 0x01 概述 最近在做审计和WAF规则的编写,在CNVD和CNNVD等漏洞平台寻找各类CMS漏洞研究编写规则时顺便抽空对 ...