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. java框架---->commonmark的使用(一)

    commonmark-java是一个Markdown 解析器,一个基于CommonMark规范解析和渲染Markdown文本的Java库.偶尔要回头看看,否则永远都在追寻,而不知道自己失去了什么. c ...

  2. Esper学习之八:EPL语法(四)

    关于EPL,已经写了三篇了,预估计了一下,除了今天这篇,后面还有5篇左右.大家可别嫌多,官方的文档对EPL的讲解有将近140页,我已经尽量将废话都干掉了,再配合我附上的例子,看我的10篇文章比那140 ...

  3. Python读写txt文本文件

    一.文件的打开和创建 ? 1 2 3 4 5 >>> f = open('/tmp/test.txt') >>> f.read() 'hello python!\n ...

  4. fs-extra 文件管理

    一.fs-extra 文件管理 $npm install fs-extra --save 1.创建一个目录 fs.mkdir(path, [mode], [callback(err)]) path 将 ...

  5. ansible register基础使用讲解

    当我们需要判断对执行了某个操作或者某个命令后,如何做相应的响应处理(执行其他 ansible 语句),则一般会用到register . 举个例子: 我们需要判断 zip 包是否存在,如果存在了就执行一 ...

  6. 【咸鱼教程】Egret中可长按复制的文本(例如复制优惠码)

    一 实际效果二 实现原理三 源码下载 在egret中实现长按复制文本效果,一般用于复制优惠码什么的. 一 实际效果         二 实现原理 在egret的游戏元素都是绘制在canvas上的,我们 ...

  7. C# 批量上传

    完整例子下载 效果: 前台: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="d ...

  8. R缺失数据处理

    > open<-c(2529,2468,2417,NA) > high<-c(2529,2483,2419,2419) > SSEC<-data.frame(ope ...

  9. 50.TO_NUMBER 将给出的字符转换为数字

    .SYSDATE 用来得到系统的当前日期 SQL> select to_char(sysdate,dd-mm-yyyy day) from dual; TO_CHAR(SYSDATE, ---- ...

  10. java.sql.SQLException:The Network Adapter could not establish the connection

    数据库连不上了,可能数据断了或者修改IP了