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

The flattened tree should look like:

   1
\
2
\
3
\
4
\
5
\
6

SOLUTION 1:

使用递归解决,根据left是否为空,先连接left tree, 然后再连接右子树。使用一个tail 来记录链的结尾。在递归之前,先将root.left,root.right保存下来。
 /**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public void flatten(TreeNode root) {
dfs(root);
} // return : the tail of the list.
public TreeNode dfs(TreeNode root) {
if (root == null) {
return null;
} TreeNode left = root.left;
TreeNode right = root.right; // Init the root.
root.left = null;
root.right = null; TreeNode tail = root; // connect the left tree.
if (left != null) {
tail.right = left;
tail = dfs(left);
} // connect the right tree.
if (right != null) {
tail.right = right;
tail = dfs(right);
} return tail;
}
}

Leetcode:Flatten Binary Tree to Linked List 解题报告的更多相关文章

  1. 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...

  2. [LeetCode]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 e ...

  3. [leetcode]Flatten Binary Tree to Linked List @ Python

    原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, fl ...

  4. [LeetCode] 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 ...

  5. LeetCode——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 ...

  6. LeetCode - 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 ...

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

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

  8. Flatten Binary Tree to Linked List (LeetCode #114 Medium)(LintCode #453 Easy)

    114. Flatten Binary Tree to Linked List (Medium) 453. Flatten Binary Tree to Linked List (Easy) 解法1: ...

  9. 【LeetCode】Flatten Binary Tree to Linked List

    随笔一记,留做重温! Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-pl ...

随机推荐

  1. Ubuntu下查看软件版本及安装位置【转】

    Ubuntu下查看软件版本及安装位置 查看软件版本:     aptitude show xxx 也可用apt-show-versions (要先安装sudo apt-get install apt- ...

  2. Zabbix检测Mysql数据库的主从同步

    在高并发网站架构中,MySQL数据库主从同步是不可或缺的,不过经常会发生由于网络原因或者操作错误,MySQL主从经常会出现不同步的情况,那么如何监控MySQL主从同步,也变成检测网站正常运行的重要环节 ...

  3. Linux安装ElasticSearch-2.2.0-分词器插件(Mmseg)

    1.在gitpub上搜索elasticsearch-analysis,能够看到所有elasticsearch的分词器: 2.安装Mmseg分词器:https://github.com/medcl/el ...

  4. eclipse导入class文件

    右键src文件夹->build path->config build path->library->add class folder->create new folder ...

  5. [转]Github 简明教程

    如果你是一枚Coder,但是你不知道Github,那么我觉的你就不是一个菜鸟级别的Coder,因为你压根不是真正Coder,你只是一个Code搬运工. 但是你如果已经在读这篇文章了,我觉的你已经知道G ...

  6. javaweb可部署目录结构

    webApp //项目名称 -META-INF --MANIFEST.MF -WEB-INF --classes   //编译class文件 --lib  //依赖jar --web.xml -ind ...

  7. 基于prometheus监控k8s集群

    本文建立在你已经会安装prometheus服务的基础之上,如果你还不会安装,请参考:prometheus多维度监控容器 如果你还没有安装库k8s集群,情参考: 从零开始搭建基于calico的kuben ...

  8. Java:集合,Arrays工具类用法

    1. 描述 Arrays工具类提供了针对数组(Array)的一些操作,比如排序.搜索.将数组(Array)转换列表(List)等等,都为静态(static)方法: binarySearch - 使用二 ...

  9. SQL SERVER 的排序规则

    有时候查询数据库的时候会发现(比如做重名检查的时候):数据库的查询时对大小写不敏感的,也就是 A 和 a 是一样的. 也就是说 select * from tabletest where name = ...

  10. 使用vs的查找功能,简单大概的统计vs中的代码行数

    VS强大的查找功能,可以使用正则表达式来进行查找,这里统计代码行数的原理就是: 在所有指定文件中进行搜索,统计匹配的文本行数. 但是匹配的行需要满足:非注释.非空等特殊非代码行. 使用Ctrl+Shi ...