leetcode — construct-binary-tree-from-preorder-and-inorder-traversal
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的更多相关文章
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [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 ...
- 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 ...
- 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 ...
- [leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...
- 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 ...
- 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】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 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 ...
- 【题解二连发】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 ...
随机推荐
- 自己编译Android(小米5)内核并刷入(一键自动编译打包)
之前自己编译过Android系统,刷入手机.编译很简单,但坑比较大,主要是GFW埋的坑.. 编译android系统太大了,今天记下自己编译及刷入android内核的方法. 主要是看到第三方内核可以超频 ...
- TypeScript 函数-Lambads 和 this 关键字的使用
let people = { name:["a","b","c","d"], /* getName:function() ...
- Go语言基础(二)
Go语言基础(二) 跟着上篇,继续看Go基础 一.变量作用域 与C类似,有全局变量.局部变量.形参之分 package main import "fmt" // 全局变量 var ...
- TimesTen数据库的备份和恢复
建立不支持增量备份的全备份 做一个全备份,fileFull为不支持增量的全备份 $ ttbackup -type fileFull -dir /tmp/backupdir sampledb_1122( ...
- window下如何使用文本编辑器(如记事本)创建、编译和执行Java程序
window下如何使用文本编辑器(如记事本)创建Java源代码文件,并编译执行 第一步:在一个英文目录下创建一个 .text 文件 第二步:编写代码 第三步:保存文件 方法一:选择 文件>另存为 ...
- 怎么修改kodexplorer网盘下的版权
前言: 要说kodexplorer,可是个好东西,在线web管理服务器文件,着实是网站管理员的好助手.内置的adminer管理数据库,用起来也是很顺手. 这么好的工具,还是免费的.但就是页面底部有ko ...
- python—文件处理
一.文件处理流程 1.打开文件,得到文件句柄并赋值 2.通过句柄对文件进行操作 3.关闭文件 二.文件打开模式 1.r,只读,默认模式 2.w,只写 3.a,追加 4. r+.w+.x+.a+ ,可读 ...
- HBuilder git使用-建立仓库,邀请用户
1.git环境配置好后,在Github上注册好帐号 2. 创建一个Respository(代码仓库) 3.邀请其他小组用户(必须的,要不别人提交不了修改) 4.把邀请链接要COPY给其他用户 5. 其 ...
- 【RL-TCPnet网络教程】第18章 BSD Sockets基础知识
第18章 BSD Sockets基础知识 本章节为大家讲解BSD Sockets,需要大家对BSD Sockets有个基础的认识,方便后面章节Socket实战操作. (本章的知识点主要整理自 ...
- Java Bloom filter几种实现比较
英文原始出处: Bloom filter for Scala, the fastest for JVM 本文介绍的是用Scala实现的Bloom filter. 源代码在github上.依照性能测试结 ...