正常写法

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. Centos 7.x 安装 Docker-ce

    Centos 下安装 Docker-ce CentOS 7.0, CentOS 7.2: cat > /etc/yum.repos.d/docker-main.repo << -'E ...

  2. Python中元组相关知识

    下面给大家介绍以下元组的相关知识: ·元组可以看成是一个不可更改的list 1.元组的创建 # 创建空元祖 t = () print(type(t)) # 创建只有一个值的元组 # 观察可知元组中如果 ...

  3. CSS--理解块级格式上下文(BFC)

    1.BFC 定义 BFC(Block formatting context)直译为"块级格式化上下文".它是一个独立的渲染区域,只有Block-level box(块级元素)参与, ...

  4. 微软 WPC 2014 合作伙伴keynote

    本周一,2014 微软WPC (Worldwide Partner Conference) 合作者伙伴大会在美国华盛顿开幕,微软除了介绍了Azure.云端化的Office 365和Windows Ph ...

  5. Google Chrome 下载&绿化&增强

    Chrome下载 Google Chrome 已经可以在线更新,虽然比较慢! 国内常用的更新地址有两处:chromedownloads 和 shuax(耍下): https://www.chromed ...

  6. Fragment 中 ListView绑定ContextMenu

    package com.example.administrator.imbobo.controller.fragment; import android.content.BroadcastReceiv ...

  7. 数据库的IO and 数据库优化问题

    一.IO介绍 IO有四种类型:连续读,随机读,随机写和连续写,连续读写的IO size通常比较大(128KB-1MB),主要衡量吞吐量,而随机读写的IO size比较小(小于8KB),主要衡量IOPS ...

  8. [20180626]函数与标量子查询14.txt

    [20180626]函数与标量子查询14.txt --//前面看http://www.cnblogs.com/kerrycode/p/9099507.html链接,里面提到: 通俗来将,当使用标量子查 ...

  9. JAVA开发学习

    一.安装JAVA开发工具IDEA,下载Ultimate旗舰版版本,Community社区版不支持Java EE开发...... 下载地址:https://www.jetbrains.com/idea/ ...

  10. 百度纯CSS生成菜单

    首页我们打看dreamweaver或其它编辑器,创建一个名为nav的导航菜单 <div class="nav"> <ul> <li><a ...