正常写法

  1. bool HasPathSum(TreeNode root, int sum) {
  2. bool ret=false;
  3. if(root==null)return false;
  4. if(root.left==null&&root.right==null) return root.val==sum;
  5. if(root.left!=null)
  6. {
  7.  
  8. ret=ret|| HasPathSum(root.left,sum-root.val);
  9. }
  10. if(root.right!=null)
  11. {
  12.  
  13. ret=ret|| HasPathSum(root.right,sum-root.val);
  14. }
  15. return ret;
  16. }

  破坏性写法

  1. bool HasPathSum(TreeNode root, int sum) {
  2. bool ret=false;
  3. if(root==null)return false;
  4. if(root.left==null&&root.right==null) return root.val==sum;
  5. if(root.left!=null)
  6. {
  7. root.left.val+=root.val;
  8. ret=ret|| HasPathSum(root.left,sum);
  9. }
  10. if(root.right!=null)
  11. {
  12. root.right.val+=root.val;
  13. ret=ret|| HasPathSum(root.right,sum);
  14. }
  15. return ret;
  16. }

  在leetcode中第二种方法速度快,但是破坏了原树的值

LeetCode112:Path Sum的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. leetcode:Path Sum (路径之和) 【面试算法题】

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  6. [LeetCode 题解]:Path Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a bi ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. C# 6 元组应用 Part 1:方便的字典工厂方法

    首先是简单的实现: public static class CollectionExtensions { public static IDictionary<TKey, TValue> M ...

  2. 国网SGCC_UAP 反编译.class文件源代码

    SGCC_UAP和eclipse操作方式差不多,对于用惯了IDEA和Android Studio的人来说非常不方便,按住Ctrl点击类名不能查看源码. 因为jar包下都是.class文件,所以需要安装 ...

  3. recovery log直接输出到串口

    我们在调试recovery升级的时候,我们经常需要查看recovery的log,google的原始逻辑中,recovery的log并非直接输出到串口,我们需要输入命令才能获取,我们有三种方式: 第一种 ...

  4. MongoDB设置

    添加mongodb用户组 groupadd mongodb 添加mongodb用户 useradd mongodb -g mongodb 将mongodb启动项目追加入rc.local保证mongod ...

  5. 【转】如何将MySQL数据目录更改为CentOS 7上的新位置

    本文转载自:http://www.leftso.com/blog/362.html 介绍 数据库随着时间的推移而增长,有时超过了文件系统的空间.当它们与操作系统的其他部分位于同一分区上时,也可能遇到I ...

  6. NetBeans数据库笔记---三层架构

    1.创建数据库,数据表 用MySQL数据库和Navicat for MySQL工具创建表 2.创建实体类——反应表结构(列——变量) 也就是对应表建立的gets和sets方法,实体类的名字一般都与数据 ...

  7. 12个HTML和CSS必须知道的重点难点问题

    这12个问题,基本上就是HTML和CSS基础中的重点个难点了,也是必须要弄清楚的基本问题,其中定位的绝对定位和相对定位到底相对什么定位?这个还是容易被忽视的,浮动也是一个大坑,有很多细节.这12个知识 ...

  8. January 04th, 2018 Week 01st Thursday

    Just do what works for you, because there will always be someone who think differently. 就做你自己所能做的,因为 ...

  9. (下一篇博客)提示5G信道

    原本注册这个博客是要不定期更新一些产品的测试内容的 但由于一些个人原因并没有坚持去做到, 每次有点子的时候却没能来得及记下来导致很内容的缺失 接下来将关键点以图片形式 和一些摘要形式先发上来, 已做备 ...

  10. St_geometry 初始用

    数据准备 点表 CREATE TABLE point_stgeom tablespace UBOSS_STS_WAREHOUSE as select car_number, longid, latid ...