二叉树的前序、中序、后序遍历、树的深度、左右子树互换
一、前序排列
void PreorderTraversal(Tree T)//前序遍历 { if( T ) { cout<<T->index <<" "<<T->data<<endl;//根 PreorderTraversal(T->pLChild);//左 PreorderTraversal(T->pRChild);//右 } }
二、中序遍历
void InorderTraversal(Tree T)//中序遍历 { if( T ) { InorderTraversal(T->pLChild);//左 cout<<T->index <<" "<<T->data<<endl;//根 InorderTraversal(T->pRChild);//右 } }
三、后序遍历
void PostorderTraversal(Tree T)//后序遍历 { if( T ) { PostorderTraversal(T->pLChild);//左 PostorderTraversal(T->pRChild);//右 cout<<T->index <<" "<<T->data<<endl;//根 } }
四、求树的深度
int GetHeight(Tree T) { int LH,RH; if(!T) { return 0; } else { LH = GetHeight(T->Left); RH = GetHeight(T->Right); return LH > RH? ++LH : ++RH; } }

浙公网安备 33010602011771号