import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
*
* Source : https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
*
*
* Given preorder and inorder traversal of a tree, construct the binary tree.
*
* Note:
* You may assume that duplicates do not exist in the tree.
*/
public class ConstructFromPreorderAndInorder { /**
* 使用前序和中序遍历结果来恢复一颗二叉树
* preorder:root/left/right
* inorder:left/root/right
*
* 根据preorder找到root,然后根据inorder找到left,right
*
* @param preorderArr
* @param inorderArr
* @return
*/
public TreeNode build (char[] preorderArr, char[] inorderArr) {
return buildByRecursion(preorderArr, 0, preorderArr.length-1, inorderArr, 0, inorderArr.length-1);
} public TreeNode buildByRecursion (char[] preorerArr, int preStart, int preEnd, char[] inorderArr, int inStart, int inEnd) {
if(preStart > preEnd || inStart > inEnd) {
return null;
} TreeNode root = new TreeNode(preorerArr[preStart] - '0');
int rootIndex = -1;
for (int i = inStart; i <= inEnd; i++) {
if (preorerArr[preStart] == inorderArr[i]) {
rootIndex = i;
break;
}
} if (rootIndex < 0) {
return null;
}
int leftTreeSize = rootIndex - inStart;
int rightTreeSize = inEnd - rootIndex;
root.leftChild = buildByRecursion(preorerArr, preStart+1, preStart + leftTreeSize,
inorderArr, inStart, rootIndex-1);
root.rightChild = buildByRecursion(preorerArr,preEnd-rightTreeSize+1, preEnd,
inorderArr, rootIndex+1, inEnd );
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 ++;
}
//去除最后不必要的
for (int i = chs.size()-1; i > 0; i--) {
if (chs.get(i) != '#') {
break;
}
chs.remove(i);
}
} 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
*/ char[] preorderArr = new char[]{'3','9','2','1','7'};
char[] inorderArr = new char[] {'9','3','1','2','7'}; ConstructFromPreorderAndInorder constructFromPreorderAndInorder = new ConstructFromPreorderAndInorder(); TreeNode root = constructFromPreorderAndInorder.build(preorderArr, inorderArr);
List<Character> chs = new ArrayList<Character>();
constructFromPreorderAndInorder.binarySearchTreeToArray(root, chs);
System.out.println(Arrays.toString(chs.toArray(new Character[chs.size()]))); } }

leetcode — construct-binary-tree-from-preorder-and-inorder-traversal的更多相关文章

  1. LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  2. [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. Leetcode Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  4. LeetCode——Construct Binary Tree from Preorder and Inorder Traversal

    Question Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may as ...

  5. [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python

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

  6. Leetcode: Construct Binary Tree from Preorder and Inorder Traversal, Construct Binary Tree from Inorder and Postorder Traversal

    总结: 1. 第 36 行代码, 最好是按照 len 来遍历, 而不是下标 代码: 前序中序 #include <iostream> #include <vector> usi ...

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

  8. 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  9. 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

  10. 【题解二连发】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 - LeetCode Construct Binary ...

随机推荐

  1. linux gdb

    linux gdb linux 测试代码 #include <stdio.h> #include <stdlib.h> static char buf[255]; static ...

  2. vimtutor——vim官方教程

    =============================================================================== =      欢     迎     阅 ...

  3. S0.4 二值图与阈值化

    目录 二值图的定义 二值图的应用 阈值化 二值化/阈值化方法 1,无脑简单判断 opencv3函数threshold()实现 2,Otsu算法(大律法或最大类间方差法) OpenCV3 纯代码实现大津 ...

  4. centos7搭建zabbix3.0监控系统

    关闭防火墙和selinux systemctl stop firewalld.service                (停止防火墙) systemctl disable firewalld.se ...

  5. 神奇高效的Linux命令行

    一.为什么要学linux命令 Linux是由命令行组成的操作系统,精髓在命令行,无论图形界面发展到什么水平,命令行方式的操作永远是不会变的.Linux命令有许多强大的功能:从简单的磁盘操作.文件存取, ...

  6. 图解Raft之领导者选举

    图解Raft领导者选举,这里通过五张图来解答Raft选举的全过程: Raft集群各个节点之间是通过RPC通讯传递消息的,每个节点都包含一个RPC服务端与客户端,初始时启动RPC服务端.状态设置为Fol ...

  7. 微信公众平台测试号 “微信登录失败,redirect_uri域名与后台配置不一致,错误代码10003”

    设置"网页授权获取用户基本信息" 点击"修改" 弹出"OAuth2.0网页授权",注意域名不加"https://"或&q ...

  8. pymongo的操作

    实例化和插入 from pymongo import MongoClient class TestMongo: def __init__(self): client = MongoClient(hos ...

  9. What is volatile?

    What is volatile? 一次偶然的机会(java多线程电梯作业寻求多个进程分享变量的方法),接触到了volatile,因此我查阅了相关的材料,对这部分做了一些了解,在这里和大家分享一下. ...

  10. position 几个属性的作用

    //定位一般都会配合left 和 top 一起使用; //静态定位 : 元素默认位置; 不脱标 不常用position:static; //相对定位 : 相对于元素本身之前的位置进行定位;不脱标pos ...