[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 ...
随机推荐
- 【java】-- 多线程之间实现通讯
1.多线程之间如何实现通讯 1.1.什么是多线程之间通讯? 多线程之间通讯,其实就是多个线程在操作同一个资源,但是操作的动作不同. 画图演示 1.2.多线程之间通讯需求 需求:第一个线程写入(inpu ...
- block学习二:使用Block替代回调
使用Block替代回调,分为三步进行:
- web-storage-cache 使用JS数据缓存
https://github.com/WQTeam/web-storage-cache 使用WebStorageCache,只要在页面上引入下面代码即可. <script src="s ...
- Android中View的绘制流程(专题讲解)
Android中的UI视图有两种方式实现:.xml文件(实现代码和UI的分离)和代码实现. Android的UI框架基本概念: 1. Activity:基本的页面单元,Activity包含一个Wind ...
- using Sysyem.Net.Http命名空间引用不了的解决方案
1.查看.Net Framework的框架是否是在4.5之上,如果不是要下载4.5之上的目标框架. 2.在引用器里面添加using System.Net.Http命名空间 选择项目列表中的“引用”-- ...
- Java_Object
说一下java中的Object类. 1.Object: Object是java类库中的一个特殊类,也是所有类的父类. Object类定义了一些有用的方法,由于是根类,这些方法在其他类中都存在,一般是进 ...
- 自己封装element-ui树组件的过滤
前言:vue开发项目时用到了element-ui的树组件,但是发现一执行过滤事件,树就全部都展开了,为了解决这个问题,只能自己先过滤数剧,再赋值给树组件的data,就避免了一上来全部展开的尴尬. 一. ...
- day03笔记
1.list操作stus = ['xiaohei','xiaobai','xiaohuang','cxdser'] #数组.list.array#增加stus.append('原宝')#在list末尾 ...
- 【CSS 第五天】背景,边框
总结一下今天所学习的内容,如下: 背景 属性 例子或作用 background background: #00FF00 url(bgimage.gif) no-repeat fixed top; ba ...
- mybatis的配置和使用
mybatis的配置和使用 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...