正常写法

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. spring 开发 Tars

    和不使用 Spring 的 tars HelloWord 项目相比,客户端完全一样,服务端两个地方不一样 创建不使用 Spring 的 tars HelloWord 步骤: https://www.c ...

  2. ArcGIS for Server 的修改IP问题

      ArcGIS for Server 的修改IP问题   1. [arcgisserver@centos6 ~]$ vi /home/arcgisserver/serverconfig/config ...

  3. Expo大作战(二十九)--expo sdk api之registerRootComponent(注册跟组件),ScreenOrientation(屏幕切换),SecureStore,

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  4. [iOS] WSHorizontalPickerView 图片水平滚动封装

    之前这篇文章传送门本来是记录自己练手的demo的,后来很多人来问我要代码.今天就抽时间封装了一下,没有考虑太多情况,等我有空再去仔细考虑吧. 代码在:Github 用法很简单,创建对象,设置数据源,记 ...

  5. 洗礼灵魂,修炼python(16)--列表进阶话题—>上节作业讲解+copy模块,浅拷贝,深拷贝

    上节课后作业: 1.使用列表解析输出结果:[(0,0),(0,2),(2,0),(2,2)] 方法1: 方法2: 方法3: 2.使用列表生成器打印斐波那契数列 3.使用列表解析生成列表[1x2,3x4 ...

  6. 模拟开户接口,使用python脚本实现批量用户开通

    1.目的 通过模拟接口方法,实现批量用户开通 2.分析 A.接口含body和head部分,其中body中的某些变量为必填字段,包含用户的信息. B.用户信息清单可以整理成ott_after_check ...

  7. 成功清除 windows2008 内部版本7601 字眼

    cmd—>bcdedit -set testsigning off重启电脑就好了

  8. linux调度器源码分析 - 概述(一)

    本文为原创,转载请注明:http://www.cnblogs.com/tolimit/ 引言 调度器作为操作系统的核心部件,具有非常重要的意义,其随着linux内核的更新也不断进行着更新.本系列文章通 ...

  9. Python+Pandas 读取Oracle数据库

    Python+Pandas 读取Oracle数据库 import pandas as pd from sqlalchemy import create_engine import cx_Oracle ...

  10. 【PAT】B1039 到底买不买(20)(20 分)

    /* 琢磨了很久,当时还没做几道题,参考了柳婼的思路 */ #include<stdio.h> #include<string.h> char arr[1000]={'\0'} ...