1、利用前序遍历创建二叉树
假设一棵二叉树的前序遍历为:‘A’,‘B’,‘C’,’#’,‘D’,’#’,’#’,’#’,‘E’
建好的树为:
代码如下:
class CreatTreeReturnValu{
public Test.Node returnRoot;
public int used;
public CreatTreeReturnValu(Test.Node returnRoot, int used) {
this.returnRoot = returnRoot;
this.used = used;
}
}
public class Test {
public static class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
}
public static CreatTreeReturnValu creatTree(char[] preOrder){
if(preOrder.length==0){
return new CreatTreeReturnValu(null,0);
}
char rootValue=preOrder[0];
if(rootValue=='#'){
return new CreatTreeReturnValu(null,1);
}
Node node=new Node(rootValue);
char[] leftPreOrder=Arrays.copyOfRange(preOrder,1,preOrder.length);
CreatTreeReturnValu leftTree = creatTree(leftPreOrder);
char[] rightPreOrder = Arrays.copyOfRange(preOrder,leftTree.used+1,preOrder.length);
CreatTreeReturnValu rightTree = creatTree(rightPreOrder);
node.left=leftTree.returnRoot;
node.right=rightTree.returnRoot;
return new CreatTreeReturnValu(node,1+leftTree.used+rightTree.used);
}
}
2、利用前序+中序创建二叉树
首先,在前序遍历序列中的首元素是二叉树的根节点,接着,在中序遍历序列中找到此节点,那么在此节点以前的节点必为其左孩子节点,以后的必为其右孩子节点;然后,在中序遍历序列中,根节点的左子树和右子树再分别对应子树前序遍历序列的首字符确定子树的根节点,再由中序遍历序列中根节点的位置分别确定构成它们的左子树和右子树的节点; 依次类推,确定二叉树的全部节点,构造出二叉树.
public static class TreeNode{
char val;
TreeNode left;
TreeNode right;
TreeNode(char x){
this.val=x;
}
}
public static int find(char[] array,char v){
for(int i=0;i<array.length;i++){
if(array[i]==v){
return i;
}
}
return -1;
}
public static TreeNode buildTree(char[] preorder,char[] inorder){
if(preorder.length==0){
return null;
}
//1.根据前序,找到根的值,并且创建根节点
char rootVal= preorder[0];
TreeNode root=new TreeNode(rootVal);
//2.在中序中找到根的值所在的下标
int leftSize=find(inorder,rootVal);
//3.切出左子树的前序和中序
char[] leftPreOrder=Arrays.copyOfRange(preorder,1,1+leftSize);
char[] leftInOrder = Arrays.copyOfRange(inorder,0,leftSize);
root.left=buildTree(leftPreOrder,leftInOrder);
//4.切出右子树的前序和中序
char[] rightPreOrder = Arrays.copyOfRange(preorder,1+leftSize,preorder.length);
char[] righntInOrder = Arrays.copyOfRange(inorder,leftSize+1,inorder.length);
root.right=buildTree(rightPreOrder,righntInOrder);
return root;
}
3、利用中序和后序遍历创建二叉树
首先,在后序遍历序列中的最后一个元素是二叉树的根节点,接着 ,在中序遍历序列中找到此节点,那么在此节点以前的节点必为其左孩子节点,以后的必为其右孩子节点;然后,在中序遍历序列中,根节点的左子树和右子树再分别对应子树后序遍历序列的尾字符确定子树的根节点,再由中序遍历序列中根节点的位置分别确定构成它们的左子树和右子树的节点; 依次类推,构造出二叉树.
public static class TreeNode{
char val;
TreeNode left;
TreeNode right;
TreeNode(char x){
this.val=x;
}
}
public static int find(char[] array,char v){
for(int i=0;i<array.length;i++){
if(array[i]==v){
return i;
}
}
return -1;
}
//利用中序+后序遍历创建二叉树
public static TreeNode buildTree1(char[] inOrder,char[] postOrder){
if(inOrder.length==0){
return null;
}
char rootval=postOrder[postOrder.length];
TreeNode root = new TreeNode(rootval);
int leftsize=find(postOrder,rootval);
char[] leftinOrder=Arrays.copyOfRange(inOrder,0,leftsize);
char[] leftpostOrder = Arrays.copyOfRange(postOrder,0,leftsize);
root.left=buildTree1(leftinOrder,leftpostOrder);
char[] rightinOrder = Arrays.copyOfRange(inOrder,leftsize+1,inOrder.length);
char[] rightpostOrder = Arrays.copyOfRange(postOrder,leftsize,postOrder.length-1);
root.right=buildTree1(rightinOrder,rightpostOrder);
return root;
}
}