Given a binary tree, return all root-to-leaf paths.

Note: A leaf is a node with no children.

Example:

Input:

   1
/ \
2 3
\
5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3 Iteration:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
if not root:
return []
stack=[]
stack.append((root,str(root.val)))
paths=[]
while stack:
node,path=stack.pop()
if not node.left and not node.right:
paths.append(path)
else:
if node.left:
stack.append((node.left,path+"->"+str(node.left.val)))
if node.right:
stack.append((node.right,path+"->"+str(node.right.val)))
return paths

  

Recursion:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def binaryTreePaths(self, root):
"""
:type root: TreeNode
:rtype: List[str]
"""
ans=[]
def isleaf(node):
if not node.right and not node.left:
return True
return False
def findPath(node,path):
if node:
path+=str(node.val)
if isleaf(node):
ans.append(path)
else:
path+="->"
findPath(node.left,path)
findPath(node.right,path)
path=""
findPath(root,path)
return ans

  

												

[LeetCode&Python] Problem 257. Binary Tree Paths的更多相关文章

  1. 【leetcode❤python】 257. Binary Tree Paths

    深度优先搜索 # Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         se ...

  2. <LeetCode OJ> 257. Binary Tree Paths

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

  3. [LeetCode&Python] Problem 107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  4. [LeetCode&Python] Problem 563. Binary Tree Tilt

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...

  5. 【LeetCode】257. Binary Tree Paths

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

  6. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

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

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

  8. LeetCode OJ 257. Binary Tree Paths

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

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

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

随机推荐

  1. 2018-2019-2 20189206 Python3学习

    python3简明教程学习 基本概念 脚本文件: 脚本文件英文为Script.实际上脚本就是程序,一般都是由应用程序提供的编程语言.应用程序包括浏览器(javaScript.VBScript).多媒体 ...

  2. C# ToLookup

    下文参考翻译自: C#/.NET Little Wonders: The ToLookup() LINQ Extension Method 故事的背景 让我们先来创建一个简单的类来表示产品,产品有ID ...

  3. _rank

    命令 ._add rank 1000 自定义等级 `level` 等级 `name`等级描述 `prefix` 前缀名称 `gossipText` 菜单显示 `meetValue` 达到值就升级 `r ...

  4. Qgis练手

    师妹推荐了一个神器 Qgis,因为看我拿Echarts和Excel缝缝补补效率实在太低下. 还记得,以前写过一个“echarts画中国地图并上色”的笔记,那个应付一下事还行,真正需要精细画图的时候还得 ...

  5. ONOS架构-概览

    这个是阅读https://wiki.onosproject.org/display/ONOS/Architecture+Guide是顺便翻译的,目前断断续续在阅读,今天先贴一部分 概览 基于osgi, ...

  6. Linux出现wrong ELF class: ELFCLASS64

    安装软件时出现问题   ×.so.×:wrong ELF class: ELFCLASS64 ,大致的意思是软件是32位的,需要32位的 ×.so.×动态链接库,而系统是64位的所提供的该 动态链接库 ...

  7. 2018普及组摆渡车洛谷5017(dp做法)

    啦啦啦,这一篇是接上一篇的博客,上一篇是记忆化搜索,而这一篇是dp+前缀和小技巧 dp这种玄学做法我这种蒟蒻当然不是自己想出来的,参考https://blog.csdn.net/kkkksc03/ar ...

  8. Django框架(七)

    15 Django组件-中间件 中间件 中间件的概念 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的 ...

  9. 微信小程序silk格式转码成mp3格式

    最近小程序项目需要录制语音并上传到服务器,经过查资料了解 目前微信小程序录音的文件后缀名是silk,因此需要转换. 经过查资料了解,参考一下的地址 https://github.com/kn007/s ...

  10. linux svn 多项目设置

    cd /svn/repos svnadmin create project_a svnadmin create project_b cd project_a cp -a conf /svn/ cd / ...