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. 剑指offer(21)栈的压入、弹出序列

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序 ...

  2. Learning-Python【34】:进程之生产者消费者模型

    一.什么是生产者消费者模型 生产者指的是生产数据的任务,消费者指的是处理数据的任务,在并发编程中,如果生产者处理速度很快,而消费者处理速度很慢,那么生产者就必须等待消费者处理完,才能继续生产数据.同样 ...

  3. 在 mac iTerm2 中使用 cmd 终端

    在 mac iTerm2 中使用 cmd 终端 主要是因为要在 window 中做一些命令行上的工作, 但又不想切换到整个 window 系统里面去. 在程序和功能中开启 telnet 在服务中启用 ...

  4. 剑指offer 01:二维数组中的查找

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...

  5. PHP钩子的简单介绍

    <?php /** * 钩子类 */ class Hook { static public function execute($type, $model='') { if($model == ' ...

  6. 08.vue中样式-class

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Microsoft Visual Studio已停止工作

    问题:今天在安装Visual Studio时,提示“Visual Studio installer 已停止工作” 解决办法:卸载原有的 .net  framework,在微软官网下载 .net fra ...

  8. php向mariaDB插入数据时乱码问题解决 --- mysqli_set_charset(设置默认字符编码)

    参考文章: https://www.w3schools.com/php/func_mysqli_set_charset.asp http://php.net/manual/zh/mysqli.set- ...

  9. python tar 打包

    import os import tarfile def make_targz_one_by_one(output_filename, source_dir): tar = tarfile.open( ...

  10. pagex/y offsetx/y screenx/y clientx/y 用法及区别

    1  pagex/pagey:鼠标相对于整个页面的x/y坐标 注:整个页面的意思就是你整个页面的全部 例如:宽200px 高400px 那么pagex/y他们最大值就是它 2,offsetX/y与pa ...