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的更多相关文章

  1. LeetCode 257. Binary Tree Paths (二叉树路径)

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  2. [LeetCode] 257. Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  3. (easy)LeetCode 257.Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  4. Java [Leetcode 257]Binary Tree Paths

    题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

  5. [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 ...

  6. 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 ...

  7. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

  8. <LeetCode OJ> 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  9. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

随机推荐

  1. 图片上传功能<转>http://blog.csdn.net/u011159417/article/details/50126023

    以前也实现过上传,只不过每次都是,写完之后没有总结,下次遇到时,还要重新写,重新调式,很是浪费时间,所以,今天实现一个上传图片的功能,包括简单的页面和servlet,下次再要写这个功能时,直接拿过来就 ...

  2. php配置rewrite模块

    转 (1)    启用rewrite模块,在默认情况下,没有启用 修改httpd.conf文件 #启动rewrite模块 LoadModule rewrite_module modules/mod_r ...

  3. 布局 - panel

    panel一般作为其他组件的容器使用 很多组件都继承自panel 对于面板中的内容,支持异步从后台加载,当然,作为纯粹的面板,一般不会用到这个,但他的子类对于这个功能还是蛮实用的 <%@ tag ...

  4. Openjudge 1.12-04

    04:最匹配的矩阵 查看 总时间限制:  1000ms 内存限制:  65536kB 描述 给定一个m*n的矩阵A和r*s的矩阵B,其中0 < r ≤ m, 0 < s ≤ n,A.B所有 ...

  5. 教你如何调用百度编辑器ueditor的上传图片、上传文件等模块

    出于兴趣爱好,前段时间自己尝试写了一个叫simple的cms,里面使用了百度ueditor编辑器,发现它的多图片上传模块很不错,用起来很方便,又可以选择已经上传好的图片.正好我又是个懒人,发现有现成的 ...

  6. Prism中使用MEF的例子

    一个基本的例子,没有viewmodel,没有使用Behaviors 大体步骤: 1.创建应用程序 2.使用"Shell"替换"MainWindow"(silve ...

  7. scala 学习笔记(01) 函数定义、分支、循环、异常处理、递归

    package yjmyzz import scala.io.StdIn object ScalaApp { def main(args: Array[String]) { println(" ...

  8. Tomcat 项目部署方式

    方法一:在Tomcat中的Conf目录中,在Server.Xml中的,<Host/>节点中添加: <Context Path="/Hello"Docbase=&q ...

  9. 开源 XFControls , 用于 Xamarin.Forms 的自定义控件集

    从此以后不会在博客园上发表任何言论,观注我的同志们,洗洗睡吧. ---------------------- 博文移至: http://www.jianshu.com/p/3ed1a3f10955

  10. unity3d 扩展NGUI Tweener —— TweenTime

    这是今天做的一个小功能 策划想要一个时间滚动效果 那就搞呗!思路和之前写的tweenFillAmount一样 传送门:http://www.cnblogs.com/shenggege/p/479892 ...