[Leetcode]112. Path Sum -David_Lin
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 思路:递归所有路径,如果到达叶子节点,并且此时sum=0,则返回true;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root==null)
return flase; //一开始如果传进来空引用,则返回false;
sum-=root.val;
if (root.left==null&&&root.right==null){ //如果已经到了叶子
if (sum==0) //如果此时sum=0,则返回true
return true; //否则返回false
else
return false;
}
//同时递归左右子树
return hasPathSum(root.left,sum)||hasPathSum(root.right,sum);
}
}
[Leetcode]112. Path Sum -David_Lin的更多相关文章
- 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 二叉树的路径和
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 ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- 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
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 ----- java
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Java [Leetcode 112]Path Sum
题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- (二叉树 DFS 递归) 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 ...
随机推荐
- itunes 备份导致C盘空间不足问题解决办法
首先,itunes备份的默认路径是 C:\users\你的用户名\Appdata\Roaming\Apple computer 备份的主要存放文件在C:\Users\David_lu\AppData\ ...
- Java学习之软件安装
成功安装了jdk-10.0.1.eclipse-committers-2018-09-win32-x86_64.mysql-5.7.18.1和tomcat-9.0.0.M17
- JavaScript(八)
闭包 什么是闭包 函数嵌套函数,内部函数可以引用外部函数的参数和变量,参数和变量不会被垃圾回收机制收回 function aaa(a){ var b = 5; function bbb(){ a++; ...
- Shell中sed使用
sed是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往 ...
- 关于在centos7 64为引用android so引发的问题修复
背景: 公司有解码的app,解码库位c++编写so动态库. 之前做过一版在调用html5摄像头,然后提取图像进行解码,后面因为图像质量不佳放弃. 最近 因为小程序api有更新 可以获取到相对清晰的图像 ...
- 二、JAVA基础、语法
第二节:JAVA基础.语法 1.修饰符.变量: Java中主要有如下几种类型的变量 局部变量 ...
- spark streaming中维护kafka偏移量到外部介质
spark streaming中维护kafka偏移量到外部介质 以kafka偏移量维护到redis为例. redis存储格式 使用的数据结构为string,其中key为topic:partition, ...
- Katalon Studio之请求响应中文乱码解决方法
最近在用Katalon做接口测试过程中发现请求响应消息中返回的中文均为乱码,这是因为我们使用的系统环境在初始安装时选择的中文简体,导致windows系统默认编码格式为GBK,但是KS的编码格式是UTF ...
- ArcGIS Runtime For Android 100.3天地图不加载问题
ArcGIS Runtime 100.3 不加载天地图问题 参考这篇帖子:https://community.esri.com/thread/220496-1003-webtiledlayer-can ...
- 为不具有change事件的html标签设置监听事件
change事件会在文本内容或选项被更改时触发. 该事件仅适用于<input type="text">和<textarea>以及<select> ...