257. Binary Tree Paths返回所有深度优先的遍历
[抄题]:
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"]就吓懵了,其实就是字符串数组啊
[思维问题]:
以为这下需要汇总再返回了,其实不用,返回也是参数化了,在函数参数中进行的
[一句话思路]:
- 左子树非空就递归左边,右子树非空就递归右边。划分的条件是左右,以前用过但是不懂。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 叶子结点象征一条路的结束,可以把路添加到ans中。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 叶子结点象征一条路的结束,可以把路添加到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返回所有深度优先的遍历的更多相关文章
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- 257. Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Leetcode题 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
随机推荐
- Python--线性代数篇
讲解Python在线性代数中的应用,包括: 一.矩阵创建 先导入Numpy模块,在下文中均采用np代替numpy import numpy as np 矩阵创建有两种方法,一是使用np.mat函数或者 ...
- Exception in thread "main" java.lang.UnsupportedClassVersionError: com/crack
执行一个jar文件的时候抛异常了 Exception in thread "main" java.lang.UnsupportedClassVersionError: com/cr ...
- 搭建基于hyperledger fabric的联盟社区(七) --升级chaincode
上个版本的chaincode有很多功能不完备,所以要部署新版本的chaincode.Fabric支持在保留现有状态的前提对chaincode进行升级. 一.新版chaincode 新版本的chainc ...
- Python基本序列-字典
Python 基本序列-字典 字典(dict)是"键-值 对"的无序可变序列,字典中的每个元素包含两部分,"键"和"值". 字典中的&quo ...
- Java里的常用运算符及其优先级顺序
知道了八种基本数据类型后,在使用中弄清运算符的优先级是很有必要的.具体如下图: 这里需要注意的是,强制类型转换的优先级是位于乘除前面而处于单目运算符后面的,这是比较容易出错的地方.比如用Math.R ...
- Nginx RTMP 模块 nginx-rtmp-module 指令详解
译序:截至 Jul 8th,2013 官方公布的最新 Nginx RTMP 模块 nginx-rtmp-module 指令详解.指令Corertmp语法:rtmp { ... }上下文:根描述:保存所 ...
- ActiveMQ之一--ActiveMQ入门
MQ的消费-生产者模型的一个典型的代表,一端往消息队列中不断的写入消息,而另一端则可以读取或者订阅队列中的消息.MQ和JMS类似,但不同的是JMS是SUN JAVA消息中间件服务的一个标准和API定义 ...
- ehcache介绍
EHCache是来自sourceforge(http://ehcache.sourceforge.net/) 的开源项目,也是纯Java实现的简单.快速的Cache组件.EHCache支持内存和磁盘的 ...
- 淘宝开源Web服务器Tengine基本安装步骤
Tengine 是由淘宝核心系统部基于Nginx开发的Web服务器,它在Nginx的基础上,针对大访问量 网站的需求,添加了很多功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,淘宝商 ...
- Joker的运维开发之路
python 1--数据类型,流程控制 2--数据类型详细操作,文件操作,字符编码 https://mp.weixin.qq.com/s/i3lcIP82HdsSr9LzPgkqww 点开更精彩 目前 ...