[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 ...
随机推荐
- C++ 使用 curl 进行 http 请求(GET、POST、Download)的封装
修改自网路 CommonTools.h /* * CommonTools.h * * Created on: 2018年8月2日 * Author: didi */ #include <iost ...
- Annotation(注解)
注解相当于一种标记,在程序中加入注解就相当于为程序打上某种标记,没有加,则表示没有任何标记,以后,javac编译器.开发工具和其它程序可以通过反射来了解你的类及各种元素上有无何种标记,看你的程序有什么 ...
- 十分钟了解HTTPS
一.为什么要用HTTPS——HTTP协议的缺陷 通信使用明文(不加密),内容可能会被窃听 不能验证通信方的身份,所以请求和响应都有可能是攻击者发送的 数据包在由A到B的过程中,可能经历很多次路由转发, ...
- 学习之路-->大小文件读取并分页展示
1.读取小文件,并进行分页 商品|价格 飞机|1000 大炮|2000 迫击炮|1000 手枪|123 ..... lis = [] n = 10 #每页显示10条信息 with open('小文件' ...
- C#一句话判断两个List<T>是否相等
List1.All(List2.Contains) && List1.Count == List2.Count
- oracle基础语句练习
1. 创建相关表结构 Emp----员工信息表 Ename ), --姓名 Empno ), --编号 Deptno ), --所在部门 Job ), --工种(人员类别),如:manager 经理, ...
- 配置NFS固定端口
NFS启动时会随机启动多个端口并向RPC注册,为了设置安全组以及iptables规则,需要设置NFS固定端口.NFS服务需要开启 mountd,nfs,nlockmgr,portmapper,rquo ...
- 使用SSM重新开发计科院网站
一.游览 在游览器地址栏输入:http://localhost:8080/index,即访问计科院首页,由于前期对数据库以及JavaBean的设计考虑不够充分,导致后期的代码臃肿,所以项目启动时对首页 ...
- 欢迎访问我的独立博客 tracefact.net (2019.1.30)
欢迎访问我的独立博客 tracefact.net 长期以来,我都同时维护着两个博客,博客园和 tracefact.net,感觉有点分散精力,所以博客园以后不再每篇文章都同步更新了. 我会挑个别比较好的 ...
- 小程序组件化框架 WePY 在性能调优上做出的探究
作者:龚澄 导语 性能调优是一个亘古不变的话题,无论是在传统H5上还是小程序中.因为实现机制不同,可能导致传统H5中的某些优化方式在小程序上并不适用.因此必须另开辟蹊径找出适合小程序的调估方式. 本文 ...