C语言 求二叉搜索树镜像的两种方法

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 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语言)