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 思路为DFS, 只是append进入stack的时候一并append进入当前的path即可. 1. Constraints
1) can be empty 2. Ideas
DFS T: O(n) S: O(n)
1) edge case
2) stack = [(root, str(root.val))]
3) DFS, if leaf, ans.append(path)
4)return ans 3. Codes
3.1) iterable
 class Solution:
def path(self, root):
if not root: return []
ans, stack = [], [(root, str(root.val))]
while stack
node, path = stack.pop()
if not node.left and not node.right:
ans.append(path)
if node.right:
stack.append((node.right, path + "->" + node.right.val))
if node.left:
stack.append((node.left, path + "->" + node.left.val))
return ans

3.2) Recursive

 class Solution:
def path(self, root):
def helper(root, path, ans):
path += str(root.val)
if not root.left and not root.right:
ans.append(path)
if root.left:
stack.append((root.left, path + "->", ans))
if root.right:
stack.append((root.right, path + "->", ans))
if not root: return []
ans = []
helper(root, "", ans)
return ans

4.. Test cases

1) empty

2)  1

3)

   1
/ \
2 3
\
5

[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS的更多相关文章

  1. Leetcode 257 Binary Tree Paths 二叉树 DFS

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

  2. (easy)LeetCode 257.Binary Tree Paths

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

  3. leetcode@ [124] Binary Tree Maximum Path Sum (DFS)

    https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...

  4. [LeetCode] 437. Path Sum III_ Easy tag: DFS

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

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

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

  6. Leetcode 257. Binary Tree Paths

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

  7. leetcode@ [199] Binary Tree Right Side View (DFS/BFS)

    https://leetcode.com/problems/binary-tree-right-side-view/ Given a binary tree, imagine yourself sta ...

  8. LeetCode 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. Note: A leaf is a node with no children. Example ...

随机推荐

  1. 【Python3】 django2.0 url 跳转设置

    python: 3.6.4 django :  2.0 在创建应用时候.我是把 urls.py 分开了.所以在设置url跳转时候.要修改成如下模式 1  父 urls.py 里边要加上命名空间 2   ...

  2. sqlite的一个Unable to Open database file的坑爹错误

    今天,被sqlite的一个机制给坑了.本人用C语言写的cgi程序去访问sqlite数据库,读取没有问题,但是插入新纪录和更新数据就不行,在服务器上直接对数据库进行增删查改则没有任何问题.但浏览器上访问 ...

  3. grep和sed替换文件中的字符串【转】

    sed -i s/"str1"/"str2"/g `grep "str1" -rl --include="*.[ch]" ...

  4. 【大数据系列】windows下连接Linux环境开发

    一.配置文件 1.core-site.xml <configuration> <property> <name>fs.defaultFS</name> ...

  5. 解决Devexpress ChartControl的CalcHitInfo当中SeriesPoint为Null的问题

    Winform程序 ChartControl的RuntimeHitTesting属性一定要设为True. Line Series markers的Visible一定要弄成True.CalcHitInf ...

  6. [转载]几个有趣的Linux命令

      本文给大家介绍几个有趣的Linux命令. 1. pv 命令 有时候我们在电影屏幕上看到一些字幕一个个匀速显示出来,像有人在边敲键盘,边显示一样.Linux上的pv命令可以实现这种效果. 默认情况下 ...

  7. Windows Server 2012升级R2过程中意外关闭恢复原系统方法

    2012升级R2过程中强制关闭了计算机,导致再次启动后蓝屏提示"BAD_SYSTEM_CONFIG_INFO".用2012安装盘进入尝试修复失败(安全模式什么的都不用想),进入命令 ...

  8. VIM 的一些技巧

    vim配置文件 ~/.vimrc 如果没有自己创建一个即可 filetype plugin indent on #打开插件 set number #显示行号 syntax on #语法高亮 set c ...

  9. python requests模块中返回时间elapsed解析

    一.问题: Python 中requests库在发送http请求时相当方便好用,但在使用时一直受一个问题困扰,怎么才能查看请求时长呢? 自己写时间函数再相减?NO,这个方法肯定不行. 二.解决: 好吧 ...

  10. Unity3D笔记 英保通九 创建数

    Unity中创建树:可以直接通过程序自动来创建树木还可以手动创建树木(本质上在我看来就是给程序自动创建的树动动”小手术“) 一.程序自动创建树木 3.1.层次视图中创建:一个平行光.摄像机.地.数并且 ...