6-02 2,335 views
思路:
采用递归的思想。对于根节点,若其左子树或右子树不为空,则互换左、右子树,然后对于左、右子树,分别递归上述处理方法,直至叶节点。
示例:
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
typedef struct TNode { int value; TNode* lchild; TNode* rchild; }TNode,*BTree; //采用递归进行镜像转换 void MirrorTree(BTree tree) { if (tree == NULL) return; if (tree->lchild == NULL && tree->rchild == NULL) return; TNode* temp = tree->lchild; tree->lchild = tree->rchild; tree->rchild = temp; MirrorTree(tree->lchild); MirrorTree(tree->rchild); } |
版权属于: 我爱我家
原文地址: http://magicwt.com/2012/06/02/%e6%b1%82%e4%ba%8c%e5%8f%89%e6%a0%91%e7%9a%84%e9%95%9c%e5%83%8f/
转载时必须以链接形式注明原始出处及本声明。