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 ...
随机推荐
- 把ssl模块加入到已经编译好的apache中实现HTTPS
为了使Apache支持https访问,系统需要安有apache.openssl.mod_ssl.so 1.安装openssl: 基本上系统都已经安装了,在/usr/bin/openssl下,直接使用o ...
- Java Concurrency in Practice——读书笔记
Thread Safety线程安全 线程安全编码的核心,就是管理对状态(state)的访问,尤其是对(共享shared.可变mutable)状态的访问. shared:指可以被多个线程访问的变量 mu ...
- SPFA板子 (背景:Luogu P3371 单源最短路径)
Luogu P3371 单源最短路径 题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数 ...
- go 结构体
结构体声明 type Employee struct { ID int Name string Address string DoB time.Time Position string Salary ...
- vue 实现图片上传与预览,以及清除图片
vue写原生的上传图片并预览以及清除图片的效果,下面是demo,其中里面有vue获取input框value值的方法以及vue中函数之间的调用 <!DOCTYPE html> <htm ...
- Linux-1.Windows远程连接Linux的工具
1.下载工具 想要链接远程Linux服务器,就需要工具来进行连接. 工具一:连接远端Linux工具--putty(可以用xshell啥的,我懒,就弄了个这个,建议还是xshell哈,功能多,还好看) ...
- Chrome 启动全屏,并可以F11退出
新建start.bat文件,作用,打开浏览器,并模拟按下F11,全屏,内容如下: start C:\Program" "Files" "(x86)\Google ...
- c++ 读取不了hdf5文件中的字符串
问题描述: 在拿到一个hdf5文件,想用c++去读取文件中的字符串,但是会报错:read failed ps: c++读取hdf5的字符串方法见:https://support.hdfgroup.or ...
- Ajax实现带进度条的文件上传
Ajax实现带进度条的文件上传 文件上传页面运行效果 上传文件并显示进度条运行效果 代码如下; DiskFileItemFactory factory = new DiskFileItemFactor ...
- 企业IT管理员IE11升级指南【9】—— IE10与IE11的功能对比
企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...