leetcode — flatten-binary-tree-to-linked-list
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/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
*
*
* Hints:
* If you notice carefully in the flattened tree, each node's right child points to
* the next node of a pre-order traversal.
*/
public class FlattenBinaryTree {
/**
* 把一棵二叉树按照preoorder展开为一个单向链表
*
* 先使用preorder traversal将树遍历到一个list中,然后按顺序连接起来
*
* @param root
* @return
*/
public List<TreeNode> flatten (TreeNode root) {
if (root == null) {
return null;
}
List<TreeNode> list = new ArrayList<TreeNode>();
flattenByRecursion(root, list);
for (int i = 0; i < list.size() - 1; i++) {
list.get(i).leftChild = null;
list.get(i).rightChild = list.get(i+1);
}
list.get(list.size()-1).leftChild = null;
list.get(list.size()-1).rightChild = null;
return list;
}
public void flattenByRecursion (TreeNode root, List<TreeNode> list) {
if (root == null) {
return;
}
list.add(root);
flattenByRecursion(root.leftChild, list);
flattenByRecursion(root.rightChild, list);
}
/**
* 上面的方法好在简单,但是空间复杂度是O(n),还可以优化以下占用的空间
*
* 使用常数空间来完成flattern
1
/ \
2 5
\ \
3 6 <- rightTail
\
4 <- leftTail
* 假设root的左右子树都已经flatten,那么需要操作就是
* temp = root.right
* root.right = root.left
* leftTail.right = temp
* root.left = null
*
* 递归使用这种方法
* 这种情况下递归的时候需要返回tail
*
* 递归的时候需要返回tail节点
*
* @param root
* @return
*/
public TreeNode flattenByRecursion1 (TreeNode root) {
if (root == null) {
return null;
}
TreeNode leftTail = flattenByRecursion1(root.leftChild);
TreeNode rightTail = flattenByRecursion1(root.rightChild);
if (root.leftChild != null) {
TreeNode temp = root.rightChild;
root.rightChild = root.leftChild;
root.leftChild = null;
leftTail.rightChild = temp;
}
// 因为上面已经把左子树拼接在了root和root.right之间,所以先判断顺序是:是否有右子树,是否有左子树
// 右子树不为空:如果右子树不为空,表示右子树才是尾节点,因为左子树的尾节点还在右子树的上面
if (rightTail != null) {
return rightTail;
}
// 右子树为空,左子树不为空
if (leftTail != null) {
return leftTail;
}
// 左右子树都为空:返回root
return root;
}
public TreeNode flatten1(TreeNode root) {
flattenByRecursion1(root);
return root;
}
public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
}
public static void print (TreeNode root) {
List<TreeNode> list = new ArrayList<TreeNode>();
while (root != null) {
list.add(root);
root = root.rightChild;
}
System.out.println(Arrays.toString(list.toArray(new TreeNode[list.size()])));
}
private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value;
public TreeNode(int value) {
this.value = value;
}
public TreeNode() {
}
@Override
public String toString() {
return value + "";
}
}
public static void main(String[] args) {
FlattenBinaryTree flattenBinaryTree = new FlattenBinaryTree();
char[] arr = new char[]{'1','2','5','3','4','#','6'};
List<TreeNode> list = flattenBinaryTree.flatten(flattenBinaryTree.createTree(arr));
System.out.println(Arrays.toString(list.toArray(new TreeNode[list.size()])));
print(flattenBinaryTree.flatten1(flattenBinaryTree.createTree(arr)));
}
}
leetcode — flatten-binary-tree-to-linked-list的更多相关文章
- 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 ex ...
- [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 ...
- [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 ...
- 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 ...
- [leetcode]Flatten Binary Tree to Linked List @ Python
原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, fl ...
- 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 ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- 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: ...
- 【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 ...
- 【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 ...
随机推荐
- Stream闪亮登场
Stream闪亮登场 一. Stream(流)是什么,干什么 Stream是一类用于替代对集合操作的工具类+Lambda式编程,他可以替代现有的遍历.过滤.求和.求最值.排序.转换等 二. Strea ...
- Android Getting Started
Building Your First App Creating an Android Project 介绍如何Android开发环境,具体是:怎么使用Eclipse工具 Create a Proje ...
- IntelliJ IDEA 2016.2 配置Tomcat 运行Web项目
1.可能会出现的问题 Run-->Edit Configurations 中点击"+"号没有tomcat server... 解决办法:File--->Setting- ...
- 一文教你看懂大数据的技术生态圈:Hadoop,hive,spark
转自:https://www.cnblogs.com/reed/p/7730360.html 大数据本身是个很宽泛的概念,Hadoop生态圈(或者泛生态圈)基本上都是为了处理超过单机尺度的数据处理而诞 ...
- 《JavaScript高级程序设计(第3版)》阅读总结记录第二章之在HTML中使用JavaScript
本章目录: 2.1 <script> 元素 2.1.1 标签的位置 2.1.2 延迟脚本 2.1.3 异步脚本 2.1.4 在XHTML 中的用法 2.1.5 不推荐使用的语法 2.2 嵌 ...
- Oracle (分类、数据库类型、序列)
分类: 1.DDL (定义语句) create .alter .drop 不需要commit create table aaa( tt1 varchart ) 2. DML (操纵语句) lnset ...
- 基于jQuery的控件:弹框
★页面展示 ★属性 属性 值 说明 默认值 div Object jQuery对象 $('body') width Number 控件的宽度 auto height Number 控件的高度 auto ...
- React state和props使用场景
一个组件的显示状态可以由内部状态state.外部参数props所决定. props: 1.props 是从外部传进组件的参数,主要是父组件向子组件传递数据. 2.props 对于使用它的组件来说是只读 ...
- MySQL常用存储引擎及如何选择
一.MySQL的存储引擎 完整的引擎说明还是看官方文档:http://dev.mysql.com/doc/refman/5.6/en/storage-engines.html 这里介绍一些主要的引擎 ...
- data自定义属性获取方法和设置
<!--原生获取方法--> <div data-id="id=1"></div> <script> //js原生获取方法 var i ...