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 ...
随机推荐
- The open source JavaScript graphing library that powers Plotly
https://plot.ly/javascript/time-series/ https://plot.ly/javascript/ https://github.com/plotly/plotly ...
- Sybase SQL anywhere5.5
https://github.com/mono/old-code https://wiki.scn.sap.com/wiki/display/SQLANY/SQL+Anywhere+and+Micro ...
- sql server: 最短路径
--------------------------------------------------------------------- -- Road System 道路 ------------ ...
- 微信小程序 封装请求
在写小程序的时候,每个JS文件都一大堆的请求,看得自己都眼花缭乱,下面看一下怎么对请求方法进行封装. 1,方法封装,在util文件夹下新建文件,创建request.js文件,工具文件,用于对方法封装) ...
- canvas实现验证码
在通常的登录界面我们都可以看到验证码,验证码的作用是检测是不是人在操作,防止机器等非人操作,防止数据库被轻而易举的攻破. 验证码一般用PHP和java等后端语言编写. 但是在前端,用canva或者SV ...
- 虚拟机安装ubuntu18.04及其srs服务器的搭建
第一次写博客,有些地方可能不太完善. 1.安装VMware,我用的是VMware12. 2.下载Ubuntu镜像(自Ubuntu 17.10开始桌面版本不再提供32位安装镜像,Ubuntu Serve ...
- flume组件汇总 source、sink、channel
Flume Source Source类型 说明 Avro Source 支持Avro协议(实际上是Avro RPC),内置支持 Thrift Source 支持Thrift协议,内置支持 Exec ...
- vuejs组件库pk介绍
vuejs可以说是近2年多以来最火的前端框架,随之而来就产生了非常多的组件库,我们来看看其中比较著名和人气旺盛的几个 1. Vuetify-符合material design设计理念, star数量7 ...
- python-Tkinter整理总结
笔者学习Tkinter模块也有好久时间了,现将学习的做以整理. tkinter简介(一) tkinter中lable标签控件(二) tkinter中button按钮控件(三) tkinter中entr ...
- python第五十天--paramiko
python通过paramiko实现,ssh功能 import paramiko ssh =paramiko.SSHClient()#创建一个SSH连接对象 ssh.set_missing_host_ ...