java
package com.zhu.algorithms.leetcode.tree;
import com.zhu.algorithms.leetcode.base.TreeNode;
import java.util.HashMap;
public class BuildBinaryTree {
public static void main(String[] args) {
int[] preorder = {1, 2, 5, 4, 6, 7, 3, 8, 9};
int[] inorder = {5, 2, 6, 4, 7, 1, 8, 3, 9};
TreeNode root = new BuildBinaryTree().buildTreeByPreIn(preorder, inorder);
TreePrint treePrint = new TreePrint();
System.out.println("preorder:");
treePrint.preOrderPrint(root);
System.out.println();
System.out.println("inorder:");
treePrint.inOrderPrint(root);
System.out.println();
System.out.println("postorder:");
treePrint.postOrderPrint(root);
}
public TreeNode buildTreeByPreIn(int[] preorder, int[] inorder) {
TreeNode root = traversePreIn(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
return root;
}
public TreeNode traversePreIn(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) {
if (preStart > preEnd) {
return null;
}
int rootVal = preorder[preStart];
int index = 0;
for (int i = inStart; i <= inEnd; i++) {
if (inorder[i] == rootVal) {
index = i;
break;
}
}
int leftSize = index - inStart;
TreeNode root = new TreeNode(rootVal);
root.left = traversePreIn(preorder, preStart + 1, preStart + leftSize, inorder, inStart, index - 1);
root.right = traversePreIn(preorder, preStart + leftSize + 1, preEnd, inorder, index + 1, inEnd);
return root;
}
HashMap<Integer, Integer> valToIndex = new HashMap<>();
public TreeNode buildTreeByPreInMap(int[] preorder, int[] inorder) {
for (int i = 0; i < inorder.length; i++) {
valToIndex.put(inorder[i], i);
}
TreeNode root = traversePreInUseMap(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
return root;
}
public TreeNode traversePreInUseMap(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) {
if (preStart > preEnd) {
return null;
}
int rootVal = preorder[preStart];
int index = 0;
index = valToIndex.get(rootVal);
int leftSize = index - inStart;
TreeNode root = new TreeNode(rootVal);
root.left = traversePreInUseMap(preorder, preStart + 1, preStart + leftSize, inorder, inStart, index - 1);
root.right = traversePreInUseMap(preorder, preStart + leftSize + 1, preEnd, inorder, index + 1, inEnd);
return root;
}
}
c++
#include <iostream>
#include "TreeNode.cpp"
#include <vector>
#include <unordered_map>
using namespace std;
class BuildTree {
public:
TreeNode* buildTreeFromPreAndIn(vector<int>& preorder, vector<int>& inorder) {
TreeNode* root = buildTraverse(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
return root;
}
TreeNode* buildTraverse(vector<int>& preorder, int preStart, int preEnd, vector<int>& inorder, int inStart, int inEnd) {
if (preStart > preEnd) {
return nullptr;
}
int rootVal = preorder[preStart];
int index = 0;
for (int i = inStart; i <= inEnd; i++) {
if (inorder[i] == rootVal) {
index = i;
break;
}
}
TreeNode* root = new TreeNode(rootVal);
int leftSize = index - inStart;
root->left = buildTraverse(preorder, preStart + 1, preStart + leftSize, inorder, inStart, index - 1);
root->right = buildTraverse(preorder, preStart + leftSize + 1, preEnd, inorder, index + 1, inEnd);
return root;
}
unordered_map<int, int> valToIndex;
TreeNode* buildTreeFromPreAndInMap(vector<int>& preorder, vector<int>& inorder) {
for (int i = 0; i < inorder.size(); i++) {
valToIndex[inorder[i]] = i;
}
TreeNode* root = buildTraverseMap(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
return root;
}
TreeNode* buildTraverseMap(vector<int>& preorder, int preStart, int preEnd, vector<int>& inorder, int inStart, int inEnd) {
if (preStart > preEnd) {
return nullptr;
}
int rootVal = preorder[preStart];
int index = 0;
index = valToIndex[rootVal];
TreeNode* root = new TreeNode(rootVal);
int leftSize = index - inStart;
root->left = buildTraverseMap(preorder, preStart + 1, preStart + leftSize, inorder, inStart, index - 1);
root->right = buildTraverseMap(preorder, preStart + leftSize + 1, preEnd, inorder, index + 1, inEnd);
return root;
}
};
python
class BuildTree:
def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:
if not preorder:
return None
root = TreeNode(preorder[0])
index = inorder.index(root.val)
leftSize = len(inorder[:index])
root.left = self.buildTree(preorder[1:leftSize + 1], inorder[:index])
root.right = self.buildTree(preorder[leftSize + 1:], inorder[index + 1:])
return root
Go
func buildTreeFromPreAndIn(preorder []int, inorder []int) *TreeNode {
root := buildTraverse(preorder, 0, len(preorder)-1, inorder, 0, len(inorder)-1)
return root
}
func buildTraverse(preorder []int, preStart int, preEnd int, inorder []int, inStart int, inEnd int) *TreeNode {
if preStart > preEnd {
return nil
}
rootVal := preorder[preStart]
var index int
for i := inStart; i <= inEnd; i++ {
if inorder[i] == rootVal {
index = i
break
}
}
leftSize := index - inStart
root := &TreeNode{rootVal, nil, nil}
root.Left = buildTraverse(preorder, preStart+1, preStart+leftSize, inorder, inStart, index-1)
root.Right = buildTraverse(preorder, preStart+leftSize+1, preEnd, inorder, index+1, inEnd)
return root
}
func buildTree(preorder []int, inorder []int) *TreeNode {
if len(preorder) == 0 {
return nil
}
rootVal := preorder[0]
root := &TreeNode{rootVal, nil, nil}
i := 0
for ; i < len(inorder); i++ {
if inorder[i] == rootVal {
break
}
}
root.Left = buildTree(preorder[1:len(inorder[:i])+1], inorder[:i])
root.Right = buildTree(preorder[len(inorder[:i])+1:], inorder[i+1:])
return root
}