[抄题]:

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

For example, given the following binary tree:

   1
/ \
2 3
\
5

All root-to-leaf paths are:

["1->2->5", "1->3"]

[暴力解法]:

时间分析:

空间分析:

[奇葩corner case]:

[奇葩输出]:

看到["1->2->5", "1->3"]就吓懵了,其实就是字符串数组啊

[思维问题]:

以为这下需要汇总再返回了,其实不用,返回也是参数化了,在函数参数中进行的

[一句话思路]:

  1. 左子树非空就递归左边,右子树非空就递归右边。划分的条件是左右,以前用过但是不懂。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 叶子结点象征一条路的结束,可以把路添加到ans中。

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 叶子结点象征一条路的结束,可以把路添加到ans中。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

二叉树的原理是先走到最底层叶子结点,然后返回根节点开始调用

[关键模板化代码]:

 void findBT(TreeNode root, String path, List<String> ans) {
if (root.left == null && root.right == null)
ans.add(path + root.val);//add here since it's an sign of end
if (root.left != null) findBT(root.left, path + root.val + "->", ans);
if (root.right != null) findBT(root.right, path + root.val + "->", ans);
}

path ans都作为helper的参数

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param root: the root of the binary tree
* @return: all root-to-leaf paths
*/
public List<String> binaryTreePaths(TreeNode root) {
//corner case
List<String> ans = new ArrayList<>();
if (root == null) {
return ans;
}
findBT(root, "", ans);
return ans;
} void findBT(TreeNode root, String path, List<String> ans) {
if (root.left == null && root.right == null)
ans.add(path + root.val);//add here since it's an sign of end
if (root.left != null) findBT(root.left, path + root.val + "->", ans);
if (root.right != null) findBT(root.right, path + root.val + "->", ans);
}
}

257. Binary Tree Paths返回所有深度优先的遍历的更多相关文章

  1. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

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

  2. 【LeetCode】257. Binary Tree Paths

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

  3. 257. Binary Tree Paths

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

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

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

  5. 【一天一道LeetCode】#257. Binary Tree Paths

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  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题 257. Binary Tree Paths

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

  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 二叉树 DFS

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

随机推荐

  1. RabbitMQ概念

    RabbitMQ 即一个消息队列,_主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用._RabbitMQ使用的是AMQP协议,它是一种二进制协议.默认启动端口 5672. 在 ...

  2. mysql执行导入导出数据源

    mysql执行导入导出数据源 一.导出数据表结构 导出数据库建表的结构,不带数据,windows环境下,在cmd下,执行: mysqldump –no-data –u username –p* dat ...

  3. 【转】VC中MessageBox与AfxMessageBox用法与区别

    原文网址:http://blog.csdn.net/holybin/article/details/28403109 一.MessageBox()用法 1.函数原型 Messagebox函数在Win3 ...

  4. 初识C++模板元编程(Template Mega Programming)

    前言:毕设时在开源库上做的程序,但是源码看得很晕(当时导师告诉我这是模板元编程,可以不用太在乎),最近自己造轮子时想学习STL的源码,但也是一样的感觉,大致了解他这么做要干什么,但是不知道里面的机制. ...

  5. Linux route命令

    route 命令 route命令用于显示和操作IP路由表.要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现.在Linux系统中,设置路由通常是 为了解决以 ...

  6. nfs之脚气

    nfs工作模式参考 一.NFs是什么 NFS是Network File System的缩写,即网络文件系统.客户端通过挂载的方式将NFS服务器端共享的数据目录挂载到本地目录下. nfs为什么需要RPC ...

  7. MongoDB day01

    MongoDB芒果数据库 数据存储阶段 文件管理阶段(.txt .doc .xlc) 优点:数据可以长期保存:数据有一定格式化规范:可以大量存储:使用简单方便 缺点:数据一致性差:用户查找修改不方便: ...

  8. Collection集合学习(一)———Set接口与具体实现

    接口Collection: Collection是Java的一个集合框架, 也是一个根接口.JDK中没有提供此接口的任何实现,但是提供了更具体的子接口Set和List接口的实现,所有的Collecti ...

  9. 何用glmnet或lars包进行feature selection

    #datalibrary(lars)data(diabetes)attach(diabetes) #glmnetlibrary(glmnet)library(foreach)library(Matri ...

  10. [z]IE6各种不兼容问题

    http://blqw1.diandian.com/post/2012-01-12/16137399 http://www.cnblogs.com/qiangspecial/archive/2013/ ...