1、递归遍历
// 先序遍历(递归实现)-------------------------------------------------------------
/*
1. Visit the node.
1. Call itself to traverse the node’s left subtree.
3. Call itself to traverse the node’s right subtree.
4. base case: there is no node
*/
private void preOrder(Node localRoot){
if(localRoot != null)
{
visit(localRoot); // System.out.print(localRoot.iData + " ");
preOrder(localRoot.leftChild);
preOrder(localRoot.rightChild);
}
}
// 中序遍历(递归实现)-------------------------------------------------------------
private void inOrder(Node localRoot){
if(localRoot != null)
{
inOrder(localRoot.leftChild);
visit(localRoot); //System.out.print(localRoot.iData + " ");
inOrder(localRoot.rightChild);
}
}
// 后序遍历(递归实现)-------------------------------------------------------------
private void postOrder(Node localRoot){
if(localRoot != null)
{
postOrder(localRoot.leftChild);
postOrder(localRoot.rightChild);
visit(localRoot); //System.out.print(localRoot.iData + " ");
}
}
// -------------------------------------------------------------