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. HDU-1423 最长公共上升子序列(LCIS)

    问题描述: 给定两个字符串x, y, 求它们公共子序列s, 满足si < sj ( 0 <= i < j < |s|).要求S的长度是所有条件序列中长度最长的. 做过最长公共子 ...

  2. go 可变参数

    go 在参数列表中使用 ... 表示可变参数,例如: import "fmt" func Minimum(first int, others ...int) int { min : ...

  3. C++ otlv4 连接 sql server 数据库小记

    otlv4介绍: http://otl.sourceforge.net/ 测试代码 // testotlv4.cpp : 定义控制台应用程序的入口点. // #include "stdafx ...

  4. 2018面向对象程序设计(java)课程学习进度条

    周次 (阅读/编写)代码行数 发布博文量/评论他人博文数量 课余学习时间 学习收获的最大程序阅读或编程任务 1 30-50 1/0 5 九九乘法表 2 60-80 1/0 6 实验一,实验二 3 12 ...

  5. 【转】Python+opencv利用sobel进行边缘检测(细节讲解)

    #! usr/bin/env python # coding:utf-8 # 2018年7月2日06:48:35 # 2018年7月2日23:11:59 import cv2 import numpy ...

  6. UVALive 2474 Balloons in a Box(枚举)

    https://vjudge.net/contest/277824#problem/A 尤其是模拟题,三思而后敲!!! 纠错了好久,主要还是没有处理好:单点若还未放气球,其他气球可以膨胀越过它(即可以 ...

  7. css实用属性

    background-size: 100% 100%;            背景通过拉伸实现填充 自适应 overflow: hidden;                              ...

  8. 《JavaScript DOM编程艺术》学习笔记(一)

    这本书是我听说学习前端基础入门书籍,于是就开始看了,大概是从5月10号开始看的吧,一直看到现在,差不多要看完了,书是挺厚的...286页,不过比起JAVASCRIPT权威指南来说还是差多了,权威指南才 ...

  9. .NET Standard 2.0正式发布了

    亦可赛艇 前天(2017年8月14日),.NET Standard 2.0正式版终于发布了,与之相配套的.NET Core 2.0也同时正式发布,真是令人振奋. 详情请看:https://blogs. ...

  10. i春秋官网4.0上线啦 文末有福利

    爱瑞宝地(Everybody)期待了很久的 i春秋官网4.0上线啦 除了产品的功能更加完善 性能和体验也将大幅度提高 清新.舒适的视觉感受 搭配更加便捷的操作流程 只需一秒,扫码立即登录 即刻进入网络 ...