二叉树详解-2

2.反转二叉树左右子树

 BiTree  invertTree(BiTree pRoot)

{

    if (pRoot == NULL) {

        return NULL;

    }

    pRoot->lchild = invertTree(pRoot->lchild);

    pRoot->rchild = invertTree(pRoot->rchild);

    

    BiTree pTmep =  pRoot->lchild;

    pRoot->lchild =  pRoot->rchild;

    pRoot->rchild = pTmep;

    return pRoot;

}

 调用

 BiTree newTree =  invertTree(rootTree);

PreOrderTraverse(newTree); printf(" ");

 

log:70,60,50,40,30,20,10,

 

3.数的深度

/*返回树的深度*/

int GetDepth(BiTree tree)

{

    int cd,ld,rd;

    cd = ld = rd = 0;

    if(tree)

    {

        ld = GetDepth(tree->lchild);

        rd = GetDepth(tree->rchild);

        cd = (ld > rd ? ld : rd);

        return cd+1;

    }

    else

        return 0;

}

调用

 int depth = GetDepth(rootTree);

 printf("%d",depth);

log :3 

原文地址:https://www.cnblogs.com/menchao/p/4834852.html