[抄题]:

Given a binary tree, flatten it to a linked list in-place.

For example, given the following tree:

    1
/ \
2 5
/ \ \
3 4 6

The flattened tree should look like:

1
\
2
\
3
\
4
\
5
\
6

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

知道大概,以为要用helper函数,其实可以不用,直接在主函数中写

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

tree中按正常的顺序prev.right = root连总会返回原来的tree,+右中左两次取反才能形成中序链表

[一句话思路]:

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

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

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

[总结]:

tree中按正常的顺序prev.right = root连总会返回原来的tree,+右中左两次取反才能形成中序链表

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

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

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

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
TreeNode prev = null; public void flatten(TreeNode root) {
//when to exit
if (root == null) return ; //r, l , reverse
flatten(root.right);
flatten(root.left); root.right = prev;
root.left = null;
prev = root;
}
}

114. Flatten Binary Tree to Linked List 把二叉树变成链表的更多相关文章

  1. [leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表

    /* 先序遍历构建链表,重新构建树 */ LinkedList<Integer> list = new LinkedList<>(); public void flatten( ...

  2. LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)

    题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...

  3. [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展开成链表

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  4. [leetcode]114. Flatten Binary Tree to Linked List将二叉树展成一个链表

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

  5. 114. Flatten Binary Tree to Linked List -- 将二叉树转成链表(in-place单枝树)

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  6. [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展平为链表

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

  7. 114 Flatten Binary Tree to Linked List [Python]

    114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二 ...

  8. 114. Flatten Binary Tree to Linked List(M)

    . Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ...

  9. 【LeetCode】114. Flatten Binary Tree to Linked List

    Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...

随机推荐

  1. 我发起了一个 操作系统 GUI 和 Tcp / IP 包 的 开源项目 DeviceOS

    操作系统 如果 不需要 处理 复杂多样 的 硬件 兼容性, 其实 并不算 大项目, 可以算 毕业设计 . 但是, GUI 和 Tcp / IP  这两个 部分 的 实现逻辑 很多 很复杂,  这  2 ...

  2. Hbase 与mapreduce结合

    Hbase和mapreduce结合 为什么需要用mapreduce去访问hbase的数据? ——加快分析速度和扩展分析能力 Mapreduce访问hbase数据作分析一定是在离线分析的场景下应用 案例 ...

  3. Excel技巧--反向查询

    当要从左侧的表格,查询某人所在的部门时,那么需要逆向查询.VLOOKUP函数只能正向查询.可以使用Match和index函数: Match函数:查询某个值在指定区域所在的位置: Index函数:查询指 ...

  4. Nuke Python module的使用

    最近很多脚本工作都需要脱离nuke的gui环境运行,没有了script editor就必须要尝试Nuke Python module功能了.该模式可以执行大部分在GUI环境中的命令,在自动生成或者批量 ...

  5. 深度图从ros数据类型转换成opencv数据类型

    摘要:ros下,利用realsense D435采集深度图,并将其转换成opencv的数据类型. 一. RGBD图像采集 通过image_transport包,根据给定的采集速度从realsense ...

  6. 安装JVCL/JCL组件

    在安装的时候,注意要先安装JCL,我试图直接安装JVCL,提示找不到文件,先安装JCL后再安装就不存在这个问题.安装到组件面板上的安装包以D结尾,可以Install,以R结尾的只要编译就可以了. 安装 ...

  7. 读完这个我懂了JNDI

    转载自:http://私塾在线/forum/blogPost/list/1186.html NDI 是什么 JNDI是 Java 命名与目录接口(Java Naming and Directory I ...

  8. VRay材质练习(一):水、玻璃、牛奶

    软件环境 a) 3ds max 2014b) V-Ray 3.60.03 渲染效果图集 玻璃杯 玻璃杯+水 玻璃杯+牛奶 材质详细参数 一.玻璃材质 Diffuse (0,0,0), Roughnes ...

  9. HTML5 使用小结

    1.html5新增的常用元素 (a) <article.../>代表独立完整的一遍文章 (b)<section.../>对页面内容进行分块 (c)<nav.../> ...

  10. centos7安装 owncloud注意事项

    要关于selinux 用来存放文件的目录权限要修改 chown -R apache:apache my.cnf里的binlog_format=mixed前面的注释去掉,不然报mysql错!