/*
先序遍历构建链表,重新构建树
*/
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. Django 多对多表的三种创建方式

    第一种: class Book(models.Model): name = models.CharField(max_length=32) # 第一种自动创建 authors = models.Man ...

  2. Kubernetes K8S之固定节点nodeName和nodeSelector调度详解

    Kubernetes K8S之固定节点nodeName和nodeSelector调度详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-mas ...

  3. 第15.31节 PyQt(Python+Qt)入门学习:containers容器类部件GroupBox分组框简介

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 容器部件就是可以在部件内放置其他部件的部件,在Qt Designer中可以使用的容器部件有 ...

  4. Python正则表达式\W+和\W*匹配过程的深入分析

    在学习re.split函数的处理过程中,发现执行如下语句及返回与老猿预想的不一致: >>> re.split('\W*','Hello,world') ['', 'H', 'e', ...

  5. PyQt(Python+Qt)学习随笔:Qt Designer中部件的焦点策略focusPolicy设置

    在Qt Designer中可以设置部件的焦点策略,部件的焦点策略属性取值范围由枚举类型Qt.FocusPolicy来定义,该枚举类型及其含义如下表所示: 注意:经老猿测试鼠标轮滚动获取焦点,只有在鼠标 ...

  6. python 练习洗牌

    生成随机数需要引入random模块,学习下random模块中常用的几个函数: random.random() 用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.un ...

  7. 简单且实用的关闭当前应用的auto.js 代码

    function closeCurrentPackage() { // 可以稍加修改,关闭指定app let packageName = currentPackage(); app.openAppSe ...

  8. 【C++】C++ new和malloc到底哪里不一样

    作者:李春港 出处:https://www.cnblogs.com/lcgbk/p/14118782.html 目录 一.前言 二.new和malloc两者的区别 2.1 属性的区别 2.2 使用上的 ...

  9. sql 语句使用和转换json数据

    1 连接mysql import pymysql import concurrent coon=pymysql.connect(host='localhost',user='root',passwor ...

  10. 超详细分析Bootloader到内核的启动流程(万字长文)

    @ 目录 Bootloader启动流程分析 Bootloader第一阶段的功能 硬件设备初始化 为加载 Bootloader的第二阶段代码准备RAM空间(初始化内存空间) 复制 Bootloader的 ...