/*
先序遍历构建链表,重新构建树
*/
LinkedList<Integer> list = new LinkedList<>();
public void flatten(TreeNode root) {
preOrder(root);
TreeNode res = root;
list.poll();
while (!list.isEmpty())
{
res.right = new TreeNode(list.poll());
res.left = null;
res = res.right;
}
}
public void preOrder(TreeNode root)
{
if (root==null)
return;
list.offer(root.val);
preOrder(root.left);
preOrder(root.right);
}

[leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表的更多相关文章

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

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

  2. [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 ...

  3. [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 ...

  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 把二叉树变成链表

    [抄题]: Given a binary tree, flatten it to a linked list in-place. For example, given the following tr ...

  6. leetcode 114 Flatten Binary Tree to Linked List ----- java

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

  7. [LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS

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

  8. Java for 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 ...

  9. 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 ...

随机推荐

  1. 【佛山市选2013】JZOJ2020年8月7日提高组T2 树环转换

    [佛山市选2013]JZOJ2020年8月7日提高组T2 树环转换 题目 描述 给定一棵N个节点的树,去掉这棵树的一条边需要消耗值1,为这个图的两个点加上一条边也需要消耗值1.树的节点编号从1开始.在 ...

  2. Feign 自定义 ErrorDecoder (捕获 Feign 服务端异常)

    问题描述 Feign 客户端捕获不到服务端抛出的异常 问题解决 重新 ErrorDecoder 即可,比如下面例子中在登录鉴权时想使用认证服务器抛出 OAuth2Exception 的异常,代码如下: ...

  3. moviepy音视频剪辑:视频基类VideoClip子类VideoFileClip、CompositeVideoClip、ImageSequenceClip介绍

    ☞ ░ 前往老猿Python博文目录 ░ 一.引言 在<moviepy音视频剪辑:moviepy中的剪辑相关类及关系>介绍了VideoClip主要有六个直接子类(VideoFileClip ...

  4. PyQt(Python+Qt)学习随笔:QListWidget获取指定行对应项的item()方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在列表部件中,可以通过item方法获取指定行对应的项,语法如下: QListWidgetItem i ...

  5. 第十八章、QListView/Model开发

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 QListView理论上可以和所有QAbstractItemModel派生的类如QStri ...

  6. PyQt(Python+Qt)学习随笔:Qt Designer中主窗口对象dockNestingEnabled属性

    dockNestingEnabled 属性是确认主窗口的浮动部件(dock widget)是否允许嵌套的一个属性. 如果此属性为False,则浮动部件停靠区域只能包含一个浮动部件(水平或垂直).如果此 ...

  7. PyQt(Python+Qt)学习随笔:Qt Designer中spacer部件的sizeType属性

    在Designer的spacers部件中有2个部件,分别是Horizontal Spacer和Vertical Spacer,这两个部件都有sizeType属性,如图: 这个sizeType实际上与Q ...

  8. 本地代码上传到github

    一,注册Github账号 1.先注册一个账号,注册地址:https://github.com/ 2.登录后,点击start a project 3.创建一个repository name,输入框随便取 ...

  9. python xlsxwriter创建excel 之('Exception caught in workbook destructor. Explicit close() may be required for workbook.',)

    python2.7使用xlsxwriter创建excel ,不关闭xlsxwriter对象,会报错: Exception Exception: Exception('Exception caught ...

  10. css外边距重叠及避免方法

    <html lang="en"> <head> <meta charset="UTF-8"> <meta name=& ...