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 ...
随机推荐
- C# 6 元组应用 Part 1:方便的字典工厂方法
首先是简单的实现: public static class CollectionExtensions { public static IDictionary<TKey, TValue> M ...
- 国网SGCC_UAP 反编译.class文件源代码
SGCC_UAP和eclipse操作方式差不多,对于用惯了IDEA和Android Studio的人来说非常不方便,按住Ctrl点击类名不能查看源码. 因为jar包下都是.class文件,所以需要安装 ...
- recovery log直接输出到串口
我们在调试recovery升级的时候,我们经常需要查看recovery的log,google的原始逻辑中,recovery的log并非直接输出到串口,我们需要输入命令才能获取,我们有三种方式: 第一种 ...
- MongoDB设置
添加mongodb用户组 groupadd mongodb 添加mongodb用户 useradd mongodb -g mongodb 将mongodb启动项目追加入rc.local保证mongod ...
- 【转】如何将MySQL数据目录更改为CentOS 7上的新位置
本文转载自:http://www.leftso.com/blog/362.html 介绍 数据库随着时间的推移而增长,有时超过了文件系统的空间.当它们与操作系统的其他部分位于同一分区上时,也可能遇到I ...
- NetBeans数据库笔记---三层架构
1.创建数据库,数据表 用MySQL数据库和Navicat for MySQL工具创建表 2.创建实体类——反应表结构(列——变量) 也就是对应表建立的gets和sets方法,实体类的名字一般都与数据 ...
- 12个HTML和CSS必须知道的重点难点问题
这12个问题,基本上就是HTML和CSS基础中的重点个难点了,也是必须要弄清楚的基本问题,其中定位的绝对定位和相对定位到底相对什么定位?这个还是容易被忽视的,浮动也是一个大坑,有很多细节.这12个知识 ...
- January 04th, 2018 Week 01st Thursday
Just do what works for you, because there will always be someone who think differently. 就做你自己所能做的,因为 ...
- (下一篇博客)提示5G信道
原本注册这个博客是要不定期更新一些产品的测试内容的 但由于一些个人原因并没有坚持去做到, 每次有点子的时候却没能来得及记下来导致很内容的缺失 接下来将关键点以图片形式 和一些摘要形式先发上来, 已做备 ...
- St_geometry 初始用
数据准备 点表 CREATE TABLE point_stgeom tablespace UBOSS_STS_WAREHOUSE as select car_number, longid, latid ...