leetcode — construct-binary-tree-from-inorder-and-postorder-traversal
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
*
*
* Given inorder and postorder traversal of a tree, construct the binary tree.
*
* Note:
* You may assume that duplicates do not exist in the tree.
*/
public class ConstructFromInorderAndPostorder {
/**
* 根据中序和后序遍历结果构造原来的二叉树
*
* inorder:left/root/right
* postorder:left/right/root
*
* @param inorderArr
* @param postorderArr
* @return
*/
public TreeNode build (char[] inorderArr, char[] postorderArr) {
return buildByRecursion(inorderArr, 0, inorderArr.length-1, postorderArr, 0, postorderArr.length-1);
}
public TreeNode buildByRecursion (char[] inorderArr, int inStart, int inEnd,
char[] postorderArr, int postStart, int postEnd) {
if (inStart > inEnd || postStart > postEnd) {
return null;
}
TreeNode root = new TreeNode(postorderArr[postEnd] - '0');
int rootIndex = -1;
for (int i = inStart; i <= inEnd; i++) {
if (inorderArr[i] == postorderArr[postEnd]) {
rootIndex = i;
break;
}
}
if (rootIndex < 0) {
return null;
}
int leftTreeSize = rootIndex - inStart;
int rightTreeSize = inEnd - rootIndex;
root.leftChild = buildByRecursion(inorderArr, inStart, rootIndex-1,
postorderArr, postStart, postStart + leftTreeSize-1);
root.rightChild = buildByRecursion(inorderArr, inEnd-rightTreeSize+1, inEnd,
postorderArr, postStart+leftTreeSize, postEnd-1);
return root;
}
/**
* 使用广度优先遍历将数转化为数组
*
* @param root
* @param chs
*/
public void binarySearchTreeToArray (TreeNode root, List<Character> chs) {
if (root == null) {
chs.add('#');
return;
}
List<TreeNode> list = new ArrayList<TreeNode>();
int head = 0;
int tail = 0;
list.add(root);
chs.add((char) (root.value + '0'));
tail ++;
TreeNode temp = null;
while (head < tail) {
temp = list.get(head);
if (temp.leftChild != null) {
list.add(temp.leftChild);
chs.add((char) (temp.leftChild.value + '0'));
tail ++;
} else {
chs.add('#');
}
if (temp.rightChild != null) {
list.add(temp.rightChild);
chs.add((char)(temp.rightChild.value + '0'));
tail ++;
} else {
chs.add('#');
}
head ++;
}
}
private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value;
public TreeNode(int value) {
this.value = value;
}
public TreeNode() {
}
}
public static void main(String[] args) {
/*
* 3
* / \
* 9 2
* / \
* 1 7
*/
ConstructFromInorderAndPostorder constructFromInorderAndPostorder = new ConstructFromInorderAndPostorder();
char[] inorderArr = new char[] {'9','3','1','2','7'};
char[] postorderArr = new char[]{'9','1','7','2','3'};
TreeNode root = constructFromInorderAndPostorder.build(inorderArr, postorderArr);
List<Character> chs = new ArrayList<Character>();
constructFromInorderAndPostorder.binarySearchTreeToArray(root, chs);
System.out.println(Arrays.toString(chs.toArray(new Character[chs.size()])));
}
}
leetcode — construct-binary-tree-from-inorder-and-postorder-traversal的更多相关文章
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- Leetcode Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode——Construct Binary Tree from Inorder and Postorder Traversal
Question Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may a ...
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
随机推荐
- Spark环境搭建(六)-----------sprk源码编译
想要搭建自己的Hadoop和spark集群,尤其是在生产环境中,下载官网提供的安装包远远不够的,必须要自己源码编译spark才行. 环境准备: 1,Maven环境搭建,版本Apache Maven 3 ...
- Android Getting Started
Building Your First App Creating an Android Project 介绍如何Android开发环境,具体是:怎么使用Eclipse工具 Create a Proje ...
- npm几个常用命令
npm install xxx 安装模块(例如npm install express 就会默认安装express的最新版本,也可以通过在后面加版本号的方式安装指定版本,如npm install exp ...
- QT—QTextEdit控件显示日志
功能:利用QTextEdit开发一个日志显示窗口.没有太多操作,需要实现的是日志自动向上滚动,总体的日志量可以控制在x行(比如300行)以内:其他的应用功能我后面继续添加 #include <Q ...
- 查看Oracle中存储过程长时间被卡住的原因
1:查V$DB_OBJECT_CACHE SELECT * FROM V$DB_OBJECT_CACHE WHERE name='CUX_OE_ORDER_RPT_PKG' AND LOCKS!='0 ...
- angular.toJson()
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- linux 使用sh@d0ws0cks server
[root@linux-node1 ~]# cat /etc/shadowsocks.json { "server":"x.x.x.x", , "lo ...
- 东软实习<3>
今天学习过程和小节 主要对多线程,单例模式以及jdbc进行了一些深入着重的学习, 还有就是学习了如何使用java操作HDFS 主要是对于一些继承调用的使用等 1.封装JDBC,自定义范型 2.反射,自 ...
- DCOS实践分享(2):基于Docker Compose和Swarm的Docker化之路
2016 年1 月 23 日,北京史上气温最低的一天. 在下午 1 点半的时候,由 DaoCloud 赞助的 2016 年度首次 Docker Meetup 准时开始. 在这次Meetup中,我分享了 ...
- http请求抓包神器-Fiddler(记录和检查你电脑的所有http通讯)
Fiddler是做什么的,能帮助我们做什么? 1.能够监听http/httpS的流量,可以截获从浏览器或者客户端软件向服务器发送的http/https请求: 2.对截获之后的请求,我们还能够查看请求中 ...