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.
For 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.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
boolean flag=false; if(root==null)
return false; if(root.left==null&&root.right==null&&root.val==sum)
{
flag=true;
return flag;
} if(root.left!=null||root.right!=null)
{
if(root.left!=null&&flag==false)
{
flag=hasPathSum(root.left,sum-root.val);
if(flag==true)
return flag;
} if(root.right!=null&&flag==false)
{
flag=hasPathSum(root.right,sum-root.val);
if(flag==true)
return flag;
} }
return flag;
}
}
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.
代码如下:
112. Path Sum的更多相关文章
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- [LeetCode] 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 ...
- 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] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- 112. Path Sum二叉树路径和
[抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- [LeetCode] 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 ...
- LeetCode OJ 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 ...
- Leetcode 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 ...
- [LeetCode]题解(python):112 Path Sum
题目来源 https://leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree ha ...
随机推荐
- jquery删除原事件
$(document).ready(function(){ var fn = $("#exportCsv").attr( "onclick" ); // 获取原 ...
- c++普通高精加
//作为一名蒟蒻,还请诸位不要吐槽. //第一次打c++高精加,内心有点小激动. //为codevs3116 高精度练习之加法 //程序太简单,就不打注释了. #include<cstdio&g ...
- Servlet、MySQL中文乱码
1.Servlet中文乱码: 在doPost或doGet方法里,加上以下两行即可: response.setContentType("text/html;charset=UTF-8" ...
- ubuntu连接Android调试
从这周开始尝试Android开发,记下点滴. 安装JDK.下载ADT不说,连接手机调试的时候出错,一堆问号??????????.网上一查,属于典型错误.试下来,有几步比较关键,容易忽视: 1.我机器上 ...
- [vijos P1023] Victoria的舞会3
这… 本来想学习一下Tarjan算法的,没想到码都码好了发现这题不是求强连通分量而是简单的连通分量…图论基础都还给老师了啊啊啊!最后深搜通通解决! v标记是否被访问过,scc标记每个的祖先(本来想写T ...
- println与toString()
public class Test{ public static void main(String[] args) { Mankind mk=new Mankind(); System.out.p ...
- Portlet和servlet的区别
相同之处 l 都是java技术开发的web组件 l 都是由特定的容器在管理 l 都可以动态产生各种内容 l 生命周期都是由容器管理 l 和客户端的交互通过request/response机制 不同之处 ...
- linux命令每日一练习-pwd,cd
pwd显示当前路径. pwd -P没能明白什么意思, cd 进入目录 cd ..返回上级目录
- 支持.NET和移动设备的XLS读写控件XLSReadWriteII下载地址及介绍
原文来自龙博方案网http://www.fanganwang.com/product/3085转载请注明出处 读写任何单元值 数字型.字符型.布尔型以及错误型.但是你了解日期和时间型单元吗?在Exce ...
- python 第三方库 chardet
chardet是一个非常优秀的编码识别模块.chardet 是python的第三方库,需要下载和安装,放在python安装根目录\Lib\site-packages下面 import chardet ...