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 ...
 
随机推荐
- asp.net core 日志
			
日志输出是应用程序必不可少的部分,log4net,nlog这些成熟的组件在之前的项目中被广泛使用,在asp.net core的项目中没有找到与之对应的log4net版本,nlog对core提供了很好的 ...
 - Linux下命令行安装配置android sdk
			
首先, 你得有个VPN 参考以下三篇完成Android SDK的安装 https://www.digitalocean.com/community/tutorials/how-to-build-and ...
 - 64位MicrosoftOfficeWord加载EndnoteX7
			
来源:http://jingyan.baidu.com/article/fcb5aff7a08036edaa4a71d0.html Win10 64bit 安装 Office2016 64bit 加载 ...
 - 1927: [Sdoi2010]星际竞速
			
1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 2040 Solved: 1257[Submit][Stat ...
 - 关于编写Java程序让Jvm崩溃
			
今天在书上看到一个作者提出一个问题“怎样通过编写Java代码让Jvm崩溃”,我看了之后也不懂.带着问题查了一下,百度知道里面有这样一个答案: package jvm; public class Cra ...
 - 《深入理解Bootstrap》勘误
			
感谢大家 感谢大家仔细阅读本书,并给本书指出了那么多的错误,下次重印时,一定会修正. 勘误列表 ID 发行人 章节 原文 更新文 备注 1 剑衣清风(微博) 1.5选择器(p7) [att$=valu ...
 - java基础:所有参数皆是按值参数
			
c#中对于参数的传递,有二种处理方式,默认情况下:值类型的参数,按值传递(即:方法体内的参数是原值的副本):引用类型的参数,"加ref关键字后“,按引用传递(即:方法体内的参数,是对象的指针 ...
 - MYSQL查询优化
			
目前手头有个查询: SELECT LPP.learning_project_pupilID, SL.serviceID, MAX(LPPO.start_date), SUM(LPPOT.license ...
 - JavaScript:立即执行的函数表达式
			
先要理解清楚几个概念: 以下转自:http://www.cnblogs.com/TomXu/archive/2011/12/31/2289423.html 问题的核心 当你声明类似functio ...
 - sql基本命令
			
--------------------------------------------------------SQL基本命令开始----------------------------------- ...