二叉树的创建和遍历

LintCode-73.前序遍历和中序遍历树构造二叉树

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;
		}
		
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值