正常写法

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的更多相关文章

  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. element-ui 的el-button组件中添加自定义颜色和图标

    我使用的element-ui的版本是V1.4.13. 如上图所示,如果使用el-button,加颜色是可以通过设置type属性的值,加图标就设置icon属性的值. 现在产品给了一个需求,就是自定义的很 ...

  2. Apex单元测试

    单元测试类 Salesforce中为Apex语言提供了完整的单元测试流程,包括单元测试类.测试的运行和结果分析等. 单元测试类是一种特殊的Apex类,基本语法和普通的Apex类一样. 单元测试类的结构 ...

  3. Android Studio 在项目中引用第三方jar包

    在Android Studio项目中引用第三方jar包的方法: 步骤: 1.在build.gradle文件中添加如下代码: 备注:要添加在Android作用域下 sourceSets { main { ...

  4. (网页)Angular.js 中 copy 赋值与 = 赋值 区别

    转自st.gg Angular.js 中 copy 赋值与 = 赋值 区别 为什么用 $scope.user = $scope.master; $scope.master 会跟着 $scope.use ...

  5. Django 添加mdia文件目录路径

    1.settings.py MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 2.urls.py from djan ...

  6. python内置小工具

    python -m http.server # 启动一个下载服务器 echo '{"job": "developer", "job": &q ...

  7. 洗礼灵魂,修炼python(33)--面向对象编程(3)—特殊类方法__init__,公有属性,私有属性

    在上一篇博文里,传入参数时,是在实例化对象后且在调用方法时才传入参数,感觉是不是有点繁琐对吧?可以在实例化的时候就传入参数吗?可以的,这就是本篇博文的要讲到的构造器——__init__(两边双下划线) ...

  8. python编程的简洁代码

    1.列表间元素操作 L1 = [1,3,5,]L2 = [2,5,3,1,8]x = set(L1)y = set(L2)#差集print(y - x)#交集print(y&x)#并集prin ...

  9. 在ASP.NET Core 2.0 web项目中使用EntityFrameworkCore

    一.安装EFCode包 EFCore需要根据不同的数据库选择不同的数据库提供程序database provider,各数据库的包地址:https://docs.microsoft.com/zh-cn/ ...

  10. 使用 WebStorm IDE 调试 Pomelo 应用程序

    使用得心应手的IDE来开发应用程序,可以使我们的工作事半功倍.而调试则更可以让我们准确的定位BUG,发现问题.本文讲述如何使用 WebStorm 这个怪兽级JavaScript IDE来调试 Chat ...