Leetcode 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
Given the below binary tree and
sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
解法: 使用递归。在例子中,存在一个从root出发的,sum=22的路径 <=> 存在从4出发的,sum=22-4的路径 or 存在从8出发的,sum=22-8的路径
class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
""" if not root:
return False
elif root.val == sum and not root.left and not root.right:
return True
else:
return self.hasPathSum(root.right, sum - root.val) or self.hasPathSum(root.left, sum - root.val)
也可以使用DFS+backtracking
class Solution(object):
def hasPathSum(self, root, s):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if not root:
return False res = []
self.dfs(root, s, [root.val], res)
return any(res) def dfs(self, root, s, path, res): if not root.left and not root.right and sum(path) == s:
res.append(True) if root.right:
self.dfs(root.right, s, path+[root.right.val], res)
if root.left:
self.dfs(root.left, s, path+[root.left.val], res)
Leetcode 112. Path Sum的更多相关文章
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- [LeetCode] 112. 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] 112. 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] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- LeetCode 112. 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 112 Path Sum ----- java
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Java [Leetcode 112]Path Sum
题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- [Leetcode]112. Path Sum -David_Lin
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- (二叉树 DFS 递归) leetcode 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- 运维利器-ClusterShell集群管理操作记录
在运维实战中,如果有若干台数据库服务器,想对这些服务器进行同等动作,比如查看它们当前的即时负载情况,查看它们的主机名,分发文件等等,这个时候该怎么办?一个个登陆服务器去操作,太傻帽了!写个shell去 ...
- 树莓派及其他硬件平台国内外Linux镜像站全汇总
转载至segmentfault. 本文收集整理国内对于Raspberry Pi.CubieBoard.pcDuino等各种Linux开源硬件平台,所使用的Raspbian.Arch Linux ARM ...
- PAT 1007. 素数对猜想 (20)
让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数."素数对猜想"认为"存在无穷多对相邻且 ...
- jQuery插件:跨浏览器复制jQuery-zclip
jQuery-zclip是一个复制内容到剪贴板的jQuery插件,使用它我们不用考虑不同浏览器和浏览器版本之间的兼容问题.jQuery-zclip插件需要Flash的支持,使用时记得安装Adobe F ...
- 二叉树的遍历(递归,迭代,Morris遍历)
二叉树的三种遍历方法: 先序,中序,后序,这三种遍历方式每一个都可以用递归,迭代,Morris三种形式实现,其中Morris效率最高,空间复杂度为O(1). 主要参考博客: 二叉树的遍历(递归,迭代, ...
- Android各种屏幕适配原理
dip(dp): device independent pixels(设备独立像素) dip,就是把屏幕的高分成480分,宽分成320分.比如你做一条160dip的横线,无论你在320还480的模拟器 ...
- java:如何用代码控制H2 Database启动
1.纯手动start/stop package com.cnblogs.yjmyzz.h2; import java.sql.Connection; import java.sql.DriverMan ...
- struts2: config-browser-plugin 与 convention-plugin 学习
struts2被很多新手诟病的一个地方在于“配置过于复杂”,相信不少初学者因为这个直接改投Spring-MVC了.convention-plugin. config-browser-plugin这二个 ...
- Caffe学习系列(7):solver及其配置
solver算是caffe的核心的核心,它协调着整个模型的运作.caffe程序运行必带的一个参数就是solver配置文件.运行代码一般为 # caffe train --solver=*_slover ...
- 直流调速系统Modelica基本模型
为了便于在OpenModelica进行仿真,形成一个完整的仿真模型,没有使用第三方的库,参照了DrModelica的例程,按照Modelica库的开源模型定义了所用的基本元件模型. 首先给出一些基本类 ...