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的更多相关文章

  1. 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 ...

  2. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...

  7. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  8. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  9. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  10. 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: ...

随机推荐

  1. 使用handler倒计时

    package com.example.jikangwang.myapplication; import android.content.Intent; import android.os.Handl ...

  2. mysql数据库 ,java 代码巧妙结合提升系统性能。

       查询频繁的表t_yh_transport_task 保证数据量最少,增加查询效率, 常用于查询的字段增加索引, 每日定时移动数据 <!-- 医院系统预约任务历史删除定时器 --> & ...

  3. PHP命名空间与自动加载类详解

    本文实例讲述了PHP命名空间与自动加载类.分享给大家供大家参考,具体如下: 今天我要给大家介绍的是PHP的命名空间 和 自动加载类 我先简单的分开演示 在放在一起 大家请看:什么是自动加载类? 想必大 ...

  4. JAVA_AesCBC例子

    import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.Secre ...

  5. RCNN论文细节

    写在前面: 本系列笔记主要记录本人在阅读过程中的收获,尽量详细到实现层次,水平有限,欢迎留言指出问题~ 这篇文章被认为是深度学习应用于目标检测的开山之作,自然是要好好读一下的,由于文章是前些日子读的, ...

  6. Floyd算法解决多源最短路问题

    说好的写dijkstra 算法堆优化版本的,但是因为,妹子需要,我还是先把Floyd算法写一下吧!啦啦啦! 咳咳,还是说正事吧! ----------------------------------- ...

  7. LAMP构架搭建论坛

    安装MYSQL数据库服务程序       LAMP动态网站部署架构是一套由Linux+Nginx+MYSQL+PHP组成的动态网站系统解决方案,具有免费.高效.扩展性强且资源消耗低等优良特性.使用手工 ...

  8. CentOS 7 安装配置 OpenVPN 客户端

    安装 epel yum 源: $ rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm $ ...

  9. 【设计经验】5、Verilog对数据进行四舍五入(round)与饱和(saturation)截位

    一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件:Vivado2015.4.2  Matlab2016a 仿真工具:Vivado自带仿真器 二.引言 在利用 ...

  10. Hadoop 排序

    数据排序是许多实际任务在执行时要完成的第一项工作,比如学生成绩评比.数据建立索引等.这个实例和数据去重类似,都是先对原始数据进行初步处理,为进一步的数据操作打好基础. 1.实例描述 对输入文件中的数据 ...