重建二叉树(AC遇到问题)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main 
{
	//二叉树节点定义
	static class TreeNode
	{
		int data;
		TreeNode leftChild;
		TreeNode rightChild;
		public TreeNode(int data)
		{
			this.data = data;
		}
	}
	
	public static void aftOrder(TreeNode root)
	{
		if(root.leftChild != null)
		{
			aftOrder(root.leftChild);
		}
		if(root.rightChild != null)
		{
			aftOrder(root.rightChild);
		}
		System.out.print(root.data + " ");
	}
	
	public static int NodeNum = 0;
	public static TreeNode getTree(int[] preOrders, int[] midOrders, 
			int PStart, int PEnd, 
			int MStart, int MEnd)
	{
		NodeNum++;
		//构造当前的根节点
		TreeNode root = new TreeNode(preOrders[PStart]);
		//如果子树在先序或者中序中的起始位置与结束位置相同
		//表明只有一个元素了,直接返回该元素作为一个叶子节点
		if(PStart == PEnd || MStart == MEnd)
		{
			return root;
		}
		
		//在中序中寻找左右子树分割点位置
		int MidLoc = -1;
		for(int i=MStart; i<=MEnd; i++)
		{
			if(preOrders[PStart] == midOrders[i])
			{
				MidLoc = i;
				break;
			}
		}
		
		//如果没找到分割点表明不能构造二叉树
		if(MidLoc == -1)
		{
			return null;
		}
		
		//在中序中计算左右子树节点的数目
		int LeftCount = MidLoc - MStart;
		int RightCount = MEnd - MidLoc;
		
		//递归构造左子树
		if(LeftCount > 0)
		{
			//左子树在先序中的起始位置
			int LeftPStart = PStart + 1;
			//左子树在先序中的结束位置
			int LeftPEnd = LeftPStart + LeftCount - 1;
			//左子树在中序中的起始位置
			int LeftMStart = MidLoc - LeftCount;
			//左子树在中序中的结束位置
			int LeftMEnd = MidLoc - 1;
			//进入递归构造左子树
			root.leftChild = getTree(preOrders, midOrders, 
			LeftPStart, LeftPEnd, 
			LeftMStart, LeftMEnd);
		}
		
		//递归构造右子树
		if(RightCount > 0)
		{
			//右子树在先序中的起始位置
			int RightPStart = PStart + LeftCount + 1;
			//右子树在先序中的结束位置
			int RightPEnd = RightPStart + RightCount - 1;
			//右子树在中序中的起始位置
			int RightMStart = MidLoc + 1;
			//右子树在中序中的结束位置
			int RightMEnd = MidLoc + RightCount;
			//进入递归构造右子树
			root.rightChild = getTree(preOrders, midOrders, 
			RightPStart, RightPEnd, 
			RightMStart, RightMEnd);
		}
		//返回构造出来的树根
		return root;
	}
	
    public static void main(String[] args) throws IOException 
    {
    	int[] preOrders = null;
    	int[] midOrders = null;
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        while (st.nextToken() != StreamTokenizer.TT_EOF) 
        {
        	NodeNum = 0;
            int m = (int) st.nval;
            preOrders = new int[m];
            midOrders = new int[m];
            
            for (int i = 0; i < m; i++)
			{
            	st.nextToken();
            	preOrders[i] = (int) st.nval;
			}
            
            for (int i = 0; i < m; i++)
			{
            	st.nextToken();
            	midOrders[i] = (int) st.nval;
			}
            
            TreeNode root = getTree(preOrders, midOrders, 0, preOrders.length - 1, 0, midOrders.length - 1);
            
            if(NodeNum == preOrders.length && root != null)
    		{
    			aftOrder(root);
    			System.out.println();
    		}
    		else
    		{
    			System.out.println("No");
    		}
             
        }
    }
     
}










你可能感兴趣的:(二叉树)