Java for LeetCode 112 Path Sum
2024-09-01 17:32:03
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.
解题思路:
递归,JAVA实现如下:
public boolean hasPathSum(TreeNode root, int sum) {
if(root==null)
return false;
if(root.val==sum&&root.left==null&&root.right==null)
return true;
else return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);
}
最新文章
- Markdown 图片助手-MarkdownPicPicker
- Ajax Step By Step1
- yii2 数据导出 excel导出以及导出数据时列超过26列时解决办法
- cookie实现自动登录
- osg(OpenSceneGraph)学习笔记1:智能指针osg::ref_ptr<;>;
- 打开网页自动弹出qq客户端
- MogileFS
- QML Object Attributes QML对象属性
- 基于FPGA的RGB565_YCbCr_Gray算法实现
- 使用SAS和JavaScript前端上传Azure Bolb大文件
- Core Animation 文档翻译 (第八篇)—提高动画的性能
- [ZJOI2007] 矩阵游戏
- Book : <;Hands-on ML with Sklearn &; TF>; pdf/epub
- webpack4 系列教程(十五):开发模式与webpack-dev-server
- 91. Reverse Linked List 反转链表
- 什么是DDoS攻击?DDoS防御的11种方针详解
- 使用lets encrypt获取免费ssl证书
- viewpager fragment 滑动界面
- 51nod 1564 区间的价值 | 分治 尺取法
- Selenium - 设置元素等待
热门文章
- mysql count(*) 和count(1)区别
- MFC中 给基于CFormView的单文档添加背景图片
- 【GLSL教程】(四)shder的简单示例 【转】
- FenceSyne, flush, wait
- MFC 消息类型
- 性能测试脚本开发(C&;C#&;Java)
- &;lt;&;lt;Python基础教程&;gt;&;gt;学习笔记 | 第04章 | 字典
- Vue框架引入JS库的正确姿势
- idea设置自定义图片
- ThreadLocal,LinkedBlockingQueue,线程池 获取数据库连接2改进