typedef struct node{
int data;
struct node *pLeft;
struct node *pRight;
}Node;
//递归求解
void MirrorRecursive(Node *pRoot){
if(pRoot != NULL){
Node *temp = pRoot->pLeft;
pRoot->pLeft = pRoot->pRight;
pRoot->pRight = temp;
}
if(pRoot->pLeft != NULL){
MirrorRecursive(pRoot->pLeft);
}
if(pRoot->pRight != NULL){
MirrorRecursive(pRoot->pRight);
}
}
//迭代求解 利用栈或队列都是可以实现的
void MirrorRecursively(Node *pRoot){
if(pRoot != NULL){
std::stack<Node *> stackTreeNode;
stackTreeNode.push(pRoot);
while(stackTreeNode.size()){
Node *pNode = stackTreeNode.top();
stackTreeNode.pop();
if(pNode != NULL){
Node *temp = pNode->pLeft;
pNode->pLeft = pNode->pRight;
pNode->pRight = temp;
}
if(pNode->pLeft != NULL){
stack.push(pNode->pLeft);
}
if(pNode->pRight != NULL){
stack.push(pNode->pLeft);
}
}
}
}
C语言 求二叉搜索树镜像的两种方法
最新推荐文章于 2023-04-21 15:53:34 发布