您的位置:首页 > 其它

输入一颗二元查找树,将该树转换为它的镜像

2016-05-11 17:27 369 查看
即在转换后的二元查找树中,左子树的结点都大于右子树的结点。

用递归和循环两种方法完成树的镜像转换。

例如输入:

8

/ /

6 10

/ / / /

5 7 9 11

输出:

8

/ /

10 6

/ / / /

11 9 7 5

定义二元查找树的结点为:

struct BSTreeNode // a node in the binary search tree (BST)

{

int m_nValue; // value of node

BSTreeNode *m_pLeft; // left child of node

BSTreeNode *m_pRight; // right child of node

};

#include <stdio.h>

typedef struct BSTreeNode // a node in the binary search tree (BST)
{
int nValue; // value of node
struct BSTreeNode *pLeft; // left child of node
struct BSTreeNode *pRight; // right child of node
}BSTreeNode_T;

void Revertsetree(BSTreeNode_T *pRoot)
{
if(NULL == pRoot)
return;
BSTreeNode_T *p = NULL;

p = pRoot->pLeft;
pRoot->pLeft = pRoot->pRight;
pRoot->pRight = p;

if(NULL != pRoot->pLeft)
Revertsetree(pRoot->pLeft);

if(NULL != pRoot->pRight)
Revertsetree(pRoot->pRight);

p = NULL;
}

int main(void)
{

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: