package tree;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class Buildtree {
public TreeNode buildTree(int [] preorder,int [] inorder) {
return reConstructBinaryTree(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
}
//前序遍历{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6}
private TreeNode reConstructBinaryTree(int [] pre,int startPre,int endPre,int [] in,int startIn,int endIn) {
if(startPre>endPre||startIn>endIn)
return null;
TreeNode root=new TreeNode(pre[startPre]);
for(int i=startIn;i<=endIn;i++)
if(in[i]==pre[startPre]){
root.left=reConstructBinaryTree(pre,startPre+1,startPre+i-startIn,in,startIn,i-1);
root.right=reConstructBinaryTree(pre,i-startIn+startPre+1,endPre,in,i+1,endIn);
}
return root;
}
public static void main(String[] args) {
int[] preorder = {1,2,4,7,3,5,6,8};//根左右
int[] inorder = {4,7,2,1,5,3,8,6};//左根右
/*
* 1
* 2 3
* 4 5 6
* 7 8
*
*
*后:74258631
*/
Buildtree buildtree = new Buildtree();
TreeNode binaryTree = buildtree.buildTree(preorder, inorder);
System.out.print("前序:");
buildtree.preOrderNonRecursive(binaryTree);
System.out.println();
System.out.print("后序:");
buildtree.postOrderNonRecursive(binaryTree);
System.out.println();
System.out.print("中序:");
buildtree.inOrderNonRecursive(binaryTree);
}
//前序遍历
public void preOrder(TreeNode root){
if(null!=root){
System.out.print(root.getVal()+"\t");
preOrder(root.getLeft());
preOrder(root.getRight());
}
}
//前序遍历非递归的方式
public void preOrderNonRecursive(TreeNode root){
Stack<TreeNode> stack=new Stack<TreeNode>();
while(true){
while(root!=null){
System.out.print(root.getVal()+"\t");
stack.push(root);//入栈
root=root.getLeft();
}
if(stack.isEmpty()) break;
root=stack.pop();//出栈
root=root.getRight();
}
}
//中序遍历采用递归的方式
public void inOrder(TreeNode root){
if(null!=root){
inOrder(root.getLeft());
System.out.print(root.getVal()+"\t");
inOrder(root.getRight());
}
}
//中序遍历采用非递归的方式
public void inOrderNonRecursive(TreeNode root){
Stack<TreeNode> stack=new Stack<TreeNode>();
while(true){
while(root!=null){
stack.push(root);
root=root.getLeft();
}
if(stack.isEmpty())break;
root=stack.pop();
System.out.print(root.getVal()+"\t");
root=root.getRight();
}
}
//后序遍历采用递归的方式
public void postOrder(TreeNode root){
if(root!=null){
postOrder(root.getLeft());
postOrder(root.getRight());
System.out.print(root.getVal()+"\t");
}
}
//后序遍历采用非递归的方式
public void postOrderNonRecursive(TreeNode root){
Stack<TreeNode> stack=new Stack<TreeNode>();
while(true){
if(root!=null){
stack.push(root);
root=root.getLeft();
}else{
if(stack.isEmpty()) return;
if(null==stack.lastElement().getRight()){
root=stack.pop();
System.out.print(root.getVal()+"\t");
while(root==stack.lastElement().getRight()){
System.out.print(stack.lastElement().getVal()+"\t");
root=stack.pop();
if(stack.isEmpty()){
break;
}
}
}
if(!stack.isEmpty())
root=stack.lastElement().getRight();
else
root=null;
}
}
}
//层序遍历
public void levelOrder(TreeNode root){
TreeNode temp;
Queue<TreeNode> queue=new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
temp=queue.poll();
System.out.print(temp.getVal()+"\t");
if(null!=temp.getLeft())
queue.offer(temp.getLeft());
if(null!=temp.getRight()){
queue.offer(temp.getRight());
}
}
}
class TreeNode{
public int val;
public TreeNode left, right;
public TreeNode(int val) {
this.val = val;
this.left = this.right = null;
}
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
public TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
}
}