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: ...
随机推荐
- Windows下如何将一个文件夹通过Git上传到GitHub上(转)
在通过windows系统的电脑上写代码,需要将项目上传到GitHub上去.比如在Pycharm上写Django后端,整个项目是一个文件夹的形式,那么怎么才能这个文件夹通过Git命令上传到GitHub上 ...
- c++ a+b
#include<iostream> using namespace std; int main() { int a,b,sum; cin>>a>>b; sum=a ...
- jQuery 动态绑定插件livequery的用法
- 微信小程序统计分析
在微信公众平台社区看到一个不错的东西,小博统计:https://www.wxappdev.com/:用于微信小程序统计分析.
- Java变成遇到的简单乱码问题
1.乱码 --- 编码集 编码集的本质是让数字与字符产生一个映射关系,不同的编码集映射实现也不同 比如UTF-8: "中"----> -28 -72 -83 对应 ...
- UVALive 2474 Balloons in a Box(枚举)
https://vjudge.net/contest/277824#problem/A 尤其是模拟题,三思而后敲!!! 纠错了好久,主要还是没有处理好:单点若还未放气球,其他气球可以膨胀越过它(即可以 ...
- [LeetCode] Score After Flipping Matrix 翻转矩阵后的分数
We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...
- Qt5和VS2017建立开发环境,安装后新建项目找不到Qt选项!!!
最近开发win驱动和Qt5测试程序,需要建立Qt5和VS2017开发环境---对于Qt5和VS2017安装这里不做多余叙述. 参考资源很多,讲解也不错!! 这里切入正题:在VS2017中安转Qt vs ...
- 排查MongoDB CPU使用率高的问题
1.公司业务调整,把一部分数据由Redis转至MongoDB,业务在测试环境正常,生产环境上线后发现压力一上来MongoDB的服务直接把CPU占满了,和开发的同学分析了一下也参考了一下百度上类似的问题 ...
- 单点登录实现原理(SSO)
简介 单点登录是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统的保护资源,若用户在某个应用系统中进行注销登录,所有的应用系统都不能再直接访问保护资源,像一些知名的大型网站,如:淘 ...