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. 转载>>ASCII、UTF8、Uncicode编码下的中英文字符大小

    原地址:http://www.tracefact.net/CSharp-Programming/Network-Programming-Part2.aspx ASCII.UTF8.Uncicode编码 ...

  2. mysql配置文件my.cnf模板

    [client] default-character-set = utf8mb4 port = PORT socket = /srv/myPORT/run/mysql.sock [mysqld] us ...

  3. 遍历json数组实现树

    今天小颖在工作中遇到要遍历树得问题了,实现后,怕后期遇到又忘记啦,所以记录下嘻嘻,其实这个和小颖之前写过得一篇文章    json的那些事    中第4点有关json的面试题有些类似. 数组格式: v ...

  4. [Sdoi2016]平凡的骰子

    描述 这是一枚平凡的骰子.它是一个均质凸多面体,表面有n个端点,有f个面,每一面是一个凸多边形,且任意两面不共面.将这枚骰子抛向空中,骰子落地的时候不会发生二次弹跳(这是一种非常理想的情况).你希望知 ...

  5. docker参数expose使用

    expose # 暴露端口,但不映射到宿主机,只被连接的服务访问.仅可以指定内部端口为参数 expose: - " - "

  6. docker 参数

    -a, --attach=[] Attach to STDIN, STDOUT or STDERR 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项 --add-host= ...

  7. Python中的str与bytes之间的转换的三种方法

    # bytes object b = b"example" # str object s = "example" # str to bytes sb = byt ...

  8. HTML5 Canvas 画虚线组件

    前段时间由于项目需要,用到了HTML5 Canvas画图,但是没有画虚线的方法,自己写了一个HTML5 画虚线的组件. dashedLine.js if (window.CanvasRendering ...

  9. 安装 SQL SERVER MsiGetProductInfo 无法检索 Product Code 1605错误 解决方案

    重装数据库服务器上的SQL SERVER 2008 上遇到了以下问题 标题: SQL Server 安装程序失败. SQL Server 安装程序遇到以下错误: MsiGetProductInfo 无 ...

  10. 获取页面所有链接的JS

    写了一个实用的JS脚本,获取当前页面所有的JS: var str = " \n"; var list = document.getElementsByTagName("a ...