/*
先序遍历构建链表,重新构建树
*/
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. InnoDB 中的缓冲池(Buffer Pool)

    本文主要说明 InnoDB Buffer Pool 的内部执行原理,其生效的前提是使用到了索引,如果没有用到索引会进行全表扫描. 结构 在 InnoDB 存储引擎层维护着一个缓冲池,通过其可以避免对磁 ...

  2. 老猿学5G扫盲贴:3GPP规范中部分与计费相关的规范序列文档

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 经咨询华为公司的相关专家,以及结合3GPP目录下载 ...

  3. print(end="\r") 滚动输出到屏幕

    for i in range(10000): print(i, end = "\r") print(end="\r") 滚动输出到屏幕

  4. https中间人攻击

    攻击过程: 服务器向客户端发送公钥. 攻击者截获公钥,保留在自己手上. 然后攻击者自己生成一个[伪造的]公钥,发给客户端. 客户端收到伪造的公钥后,生成加密hash值发给服务器. 攻击者获得加密has ...

  5. Codeforces Edu Round 48 A-D

    A. Death Note 简单模拟,可用\(\%\)和 \(/\)来减少代码量 #include <iostream> #include <cstdio> using nam ...

  6. 【译】为什么Rust中的BTreeMap没有with_capacity()方法?

    原文标题:Why doesn't Rust's BTreeMap have a with_capacity() method? 原文链接:https://www.nicolas-hahn.com/20 ...

  7. KVM初体验之virt-manager unable to connect to libvirt的处理办法

    解决方法 需要用root身份运行virt-manager

  8. jfinal 导出excle

    Controller Map<String,List<PoiUtilHeader>> result = new HashMap<String, List<PoiUt ...

  9. Servlet中获取请求参数问题

    1.GET方法,可以通过getParamter方法反复获取同一个变量的数据: 2.POST方法,需要注意请求类型(content-Type)是否是application/x-www-form-urle ...

  10. MobaXterm无法退格删除

    MobaXterm退格删除出现^H,总是要取消输入重新敲语句,很麻烦 解决方法:打开MobaXterm-->settings-->Configuration,把"Backspac ...