|
一、深度优先周游二叉树
1、我们变换一下根结点的周游顺序,可以得到以下三种方案:
(1) 前序周游(tLR次序):访问根结点;前序周游左子树;前序周游右子树。
(2) 中序周游(LtR次序):中序周游左子树;访问根结点;中序周游右子树。
(3) 后序周游(LRt次序):后序周游左子树;后序周游右子树;访问根结点。
2、深度优先周游的递归实现:
template<class T> void BinaryTree<T>::DepthOrder(BinaryTreeNode<T>* root){
if(root!=NULL){
Visit(root); //前序
DepthOrder(root->leftchild()); //访问左子树
Visit(root); //中序
DepthOrder(root->rightchild());//访问右子树
Visit(root); //后序
}
}
|