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 ...
随机推荐
- 雕刻机制作 PCB 指南
之前使用过感光蓝油制作过 PCB,虽然感光法精度高,但个人制作耗时耗力,发给厂家周期又很长.看到国外的网友使用雕刻机制作 PCB 视频之后.几番周折之后还是成功了.有感于网上几乎没有一份完整的雕刻机 ...
- PHP如何搭建百度Ueditor富文本编辑器
本文为大家分享了PHP搭建百度Ueditor富文本编辑器的方法,供大家参考,具体内容如下 下载UEditor 官网:下载地址 将下载好的文件解压到thinkphp项目中,本文是解压到PUBLIC目录下 ...
- ajax(2)
AJAX全称: Asynchronous JavaScript and XML ( 异步的JavaScript 和 XML) Ajax的本质就是:XMLHttpRequest 对象: 案例: v ...
- (转)protein 数据库
最早关注蛋白质互作网络,是在来GDMC第一年的时候,中间停了半年看互作-各种算法,网络分析停滞不前,没想到搞到最后,还是和网络碰到了一起,我总是会潜意识走近给自己第一印象不错的object,包括人.用 ...
- [LeetCode] New 21 Game 新二十一点游戏
Alice plays the following game, loosely based on the card game "21". Alice starts with 0 p ...
- pyhton 监听文件输入实例
def tail(filename): f = open(filename,encoding='utf-8') while True: line = f.readline() if line.stri ...
- python 用正则处理日志实例
前提: 了解正则基本语法 import re with open('top10_xiaozhuang_net.log','r') as f1: #读取日志文件 subject=f1.rea ...
- 在win10环境下搭建 solr 开发环境
在win10环境下搭建 solr 开发环境 2017年05月30日 09:19:32 SegaChen0130 阅读数:1050 在win10环境下搭建 solr 开发环境 安装环境 Windo ...
- cmd 创建用户,并授权管理员权限就可以远程登陆了
创建账号 net user 用户名 密码 /add //注意空格 授权管理员权限 net localgroup Administrators 用户名 /add // ...
- ArcMap AddIn之下载ArcGIS Server地图服务中的数据
涉及到开发知识点1.ArcGIS Server地图服务 2.C# web请求获取数据 3.AddIN开发技术 工具界面: 具体涉及到的代码之后有空贴出来.先上工具 AddIn插件下载地址:点击这里下载 ...