Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
["1->2->5", "1->3"]
本题可以用DFS或者BFS。
解法一: DFS
class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if not root:
return [] result = []
self.dfs(root, result, [str(root.val)])
return result def dfs(self, root, result, path):
if root.left is None and root.right is None:
result.append('->'.join(path))
return if root.right:
path.append(str(root.right.val))
self.dfs(root.right, result, path)
path.pop()
if root.left:
path.append(str(root.left.val))
self.dfs(root.left, result, path)
path.pop()
这个解法中需要注意的是current path的初始值是[str(root.val)]而不是。这个很类似的解法使用了pop。
如果要不使用pop的话
class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if not root:
return [] result = []
self.dfs(root, result, [str(root.val)])
return result def dfs(self, root, result, path):
if root.left is None and root.right is None:
result.append('->'.join(path))
return if root.right:
self.dfs(root.right, result, path+[str(root.right.val)])
if root.left:
self.dfs(root.left, result, path+[str(root.left.val)])
Leetcode 257. Binary Tree Paths的更多相关文章
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Java [Leetcode 257]Binary Tree Paths
题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- [leetcode]257. Binary Tree Paths二叉树路径
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- LeetCode 257. Binary Tree Paths(二叉树根到叶子的全部路径)
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
随机推荐
- php的一些小细节
1.今天看见 $arr3 = array_filter($arr, create_function('$v', 'return strlen($v);')); 作用就是去掉为空的元素,其实当callb ...
- linux基本工具使用(二)
1 查找某个目录下面一个所有的可执行文件,并且删除(对删除一个工程的可执行文件格外有用) find . -maxdepth 1 -file f -perm -111 | xargs rm
- 前端面试——css篇
css盒子模型 在W3C模型中: 总宽度 = margin-left + border-left + padding-left + width + padding-right + border-rig ...
- AngularJS中的缓存
欢迎大家指导与讨论 : ) 缓存篇 一个缓存就是一个组件,它可以透明地储存数据,以便以后可以更快地服务于请求.多次重复地获取资源可能会导致数据重复,消耗时间.因此缓存适用于变化性不大的一些数据,缓存能 ...
- AR 不同 继承映射的问题总结
在使用AR(Nhibernate) 做ORM时,使用类的继承体系时,它有不同的映射方式,解决的问题不同,带来的问题差异也很大. 1.所有数据 存储在一张表,不同的类使用 DiscriminatorCo ...
- 微信公众平台开发(71)OAuth2.0网页授权
微信公众平台开发 OAuth2.0网页授权认证 网页授权获取用户基本信息 作者:方倍工作室 微信公众平台最近新推出微信认证,认证后可以获得高级接口权限,其中一个是OAuth2.0网页授权,很多朋友在使 ...
- 完整的社交app源码android+laravel
等想到写点什么的时候再写吧,其他看代码. https://github.com/huijimuhe/monolog-android https://github.com/huijimuhe/monol ...
- 知乎日报win10版 - 天天读报【开源】
业余时间写的一个知乎日报win10版客户端,支持收藏,评论,点赞等. 商店地址:https://www.microsoft.com/zh-cn/store/apps/%E5%A4%A9%E5%A4%A ...
- css+div 浮动分块
前段时间学过几天html,只是怀着了解的态度,能够读懂别人的页面,能够扒现成就行,一直没有自己动手去实践过,其实也不是没有实践过,前段时间扒了一个网页,想按照自己的要求来改,可后果是越改越乱.今天心血 ...
- Crowdsourcing(众包)
群众外包(英语:crowdsourcing)是互联网带来的新的生产组织形式.<连线>(Wired)杂志记者Jeff Howe于2006年发明的一个专业术语,用来描述一种新的商业模式,即企业 ...