C言語における再帰的よくあるエラー解析(1)

1763 ワード

1.コードを直接修正しないでください.そうしないと、次のプログラムにエラーが発生します.【必要な値を正しく読み込めません】
2.コード例
BiTree* foundData(int postLeft,int postRight,int inLeft,int inRight){
	int i,root;//            
	int rootIndex;//       inOrder       
	root = postOrder[postRight];//      
	for(i = inLeft;i <= inRight;i++){//              !! 
		/**
		1.        ,                ,              
		   。 
		**/ 
		if(inOrder[i] == root ){ 
			rootIndex = i;//          
			break;//        
		}
	}
	//        
	BiTree* T = new BiTree;//      	
	T->data  = root;//       			 
	//       
	if(rootIndex > inLeft){//        		
		int leftTreeNumber ;//        
		leftTreeNumber = rootIndex - inLeft; 
		postRight =  postLeft + leftTreeNumber - 1;//postRight  leftTreeNumber 
		inRight = rootIndex - 1;		
		T->lChild=foundData(postLeft,postRight,inLeft,inRight);//    		
	}
	//        
	if(rootIndex < inRight){//     			
		postRight = postRight -1; 
		inLeft = rootIndex + 1; 
		T->rChild=foundData(postLeft,postRight,inLeft,inRight);//     
	}
	return T;//  		
}
3.ぶんせき
上のこのツリーを作成する過程で、これは再帰関数であり、左サブツリーを作成した後、右サブツリーを作成する必要があるかもしれませんが、左サブツリーを作成するときにpostRightおよびinRightの値を変更すると、if(rootIndex)
4.まとめ
(1)再帰プログラムでは,変数の値を変更する必要がある場合は,変更後に呼び出すのではなく,関数呼び出し中に値を直接呼び出すことが望ましい.