11 9 7 5
思路:这也是一个递归的题目,对于当前节点,如果不是NULL节点或者叶子节点,那么只需要交换左右子节点即可。这样再次递归调用左右子树。
void ConvertMirror(BinTree* root)
{
if(root == NULL ||(root->left == NULL &&root->right == NULL))
return ;
BinTree* temp = NULL;
temp = root->left;
root->left = root->right;
root->right = temp;
if(root->left != NULL)
ConvertMirror(root->left);
if(root->right != NULL)
ConvertMirror(root->right);
}void ConvertMirrorNoIter(BinTree* root)
{
stack<BinTree*> st;
if(root == NULL)
return ;
st.push(root);
BinTree* temp;
while(!st.empty())
{
temp = st.top();
st.pop();
BinTree* cur = temp->left;
temp->left = temp->right;
temp->right = cur;
if(temp->left != NULL)
st.push(temp->left);
if(temp->right != NULL)
st.push(temp->right);
}
}
原文地址:http://blog.csdn.net/yusiguyuan/article/details/45361649