LeetCode112:Path Sum
正常写法
bool HasPathSum(TreeNode root, int sum) {
bool ret=false;
if(root==null)return false;
if(root.left==null&&root.right==null) return root.val==sum;
if(root.left!=null)
{
ret=ret|| HasPathSum(root.left,sum-root.val);
}
if(root.right!=null)
{
ret=ret|| HasPathSum(root.right,sum-root.val);
}
return ret;
}
破坏性写法
bool HasPathSum(TreeNode root, int sum) {
bool ret=false;
if(root==null)return false;
if(root.left==null&&root.right==null) return root.val==sum;
if(root.left!=null)
{
root.left.val+=root.val;
ret=ret|| HasPathSum(root.left,sum);
}
if(root.right!=null)
{
root.right.val+=root.val;
ret=ret|| HasPathSum(root.right,sum);
}
return ret;
}
在leetcode中第二种方法速度快,但是破坏了原树的值
LeetCode112:Path Sum的更多相关文章
- Project Euler 83:Path sum: four ways 路径和:4个方向
Path sum: four ways NOTE: This problem is a significantly more challenging version of Problem 81. In ...
- Project Euler 82:Path sum: three ways 路径和:3个方向
Path sum: three ways NOTE: This problem is a more challenging version of Problem 81. The minimal pat ...
- Project Euler 81:Path sum: two ways 路径和:两个方向
Path sum: two ways In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom ...
- LeetCode之“树”:Path Sum && Path Sum II
Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...
- leetcode:Path Sum (路径之和) 【面试算法题】
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- [LeetCode 题解]:Path Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- LeetCode OJ:Path Sum II(路径和II)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- LeetCode OJ: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 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
随机推荐
- SDK Manager
dx.bat :将所有的.class文件变成一个.dex文件. aapt:Android Application package tools 安卓应用的打包工具. adb:Android Debug ...
- unresolved external symbol boost::throw_exception
使用boost库,VS生成的时候一直报错, error LNK2019: 无法解析的外部符号 "void __cdecl boost::throw_exception(class std:: ...
- Redis系列(二):Redis的数据类型及命令操作
原文链接(转载请注明出处):Redis系列(二):Redis的数据类型及命令操作 Redis 中常用命令 Redis 官方的文档是英文版的,当然网上也有大量的中文翻译版,例如:Redis 命令参考.这 ...
- python 3.3.2报错:No module named 'urllib2'
ModuleNotFoundError: No module named 'urllib3' 1. ImportError: No module named 'cookielib'1 Python3中 ...
- MD5生成
/// <summary> /// 32位MD5加密 /// </summary> /// <param name="input"></p ...
- 阿里云上安装pip3(Ubuntu)
安装pip3: 这个简单啊,到网上下载get-pip.py的脚本,然后scp到你的阿里云服务器上,python3 get-pip.py即可. 如果不会scp,哈哈,按照下面的几步: wget http ...
- kafka 配置文件参数详解
kafka的配置分为 broker.producter.consumer三个不同的配置 一 BROKER 的全局配置 最为核心的三个配置 broker.id.log.dir.zookeeper.con ...
- Spring boot + mybatis + orcale
接着上次的实现, 添加 mybatis 查询 orcale 数据库 第一步: 新建几个必须的包, 结果如下 第二步: 在service包下新建personService.java 根据名字查perso ...
- row_number() over() 一句话概括,以及max()函数的一种查询分组中最大值的用法
row_number() over(partition by col1 order by col2) 根据COL1分组可能会有多个组,每组组内根据COL2进行排序.每组内都有自动生成的序号,从1开始, ...
- JS中=>,>>>是什么意思
最近经常看到 JS中=>,符号,于是查了一下别人的博客 =>是es6语法中的arrow function 举例:(x) => x + 6 相当于 function(x){ ret ...