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 ...
随机推荐
- MongoDB集群架构及搭建
MongoDB分布式集群 MongDB分布式集群能够对数据进行备份,提高数据安全性,以及提高集群提高读写服务的能力和数据存储能力.主要通过副本集(replica)对数据进行备份,通过分片(shardi ...
- Hilbert-Huang Transform: matlab 希尔伯特-黄变换: matlab实现
关于Hilbert-Huang的matlab实现,材料汇总,比较杂...感谢所有网络上的贡献者们:) 核心:以下代码计算HHT边际谱及其对应频率 工具包要求:G-Rilling EMD Toolbox ...
- QT TCP文件上传服务器
利用QT做为client端,纯C语言做为server端,利用tcp协议,实现client端向server端传递文件 Linux服务器端 //头文件 #include <stdio.h> # ...
- document.elementFromPoint在IE8下无法稳定获取当前坐标元素的解决方法
document.elementFromPoint(e.clientX, e.clientY) document.elementFromPoint(e.clientX, e.clientY) 执行2次 ...
- 利用Weblogic的iisproxy、iisforward插件实现IIS转发
默认情况下,IIS只能提供http重定向功能,而无法满足转发需求. 举例:http://localhost/app1 利用http重定向到 http://www.abc.com/app1 访问 htt ...
- Unity 使用快速教程
Unity是微软在CodePlex上的一个开源项目,可用于依赖注入.控制反转,类似Spring,下面是使用示例: 1.先来定义几个接口.类 namespace UnityTest { public i ...
- TinyFrame续篇:整合Spring IOC实现依赖注入
上一篇主要讲解了如何搭建基于CodeFirst的ORM,并且在章节末我们获取了上下文对象的实例:BookContext.这节主要承接上一篇,来讲解如何整合Spring IOC容器实现控制反转,依赖注入 ...
- lecture11-hopfiled网络与玻尔兹曼机
Hinton课程第11课 这部分的课程算是个知识背景,讲述RBM的来源吧,毕竟是按照hopfield--BM-RBM的路线过来的. 因为水平有限,都是直译,如果纠结某句话,肯定看不懂,所以这些课程只需 ...
- Theano2.1.18-基础知识之theano的扩展
来自:http://deeplearning.net/software/theano/tutorial/extending_theano.html Extending Theano 该教程覆盖了如何使 ...
- fir2(n,f,m)
编辑 函数fir2用来设计多通带任意响应FIR滤波器,该滤波器的幅频特性由向量对f和m确定,f为归一化频率向量,m为对应频率点上的幅度.当设计的滤波器在频率为π的幅度响应不是0时,滤波器的阶数n为偶数