二叉树的翻转
题目来源:https://leetcode-cn.com/problems/invert-binary-tree/
要求:
分析:判断如果此节点为空或者此节点的左右孩子都为空,那么返回此节点,否则交换此节点的左右孩子,依次递归实现此节点的左孩子和有孩子。
代码实现:
1 void swapNode(BiTree &t); 2 BiTree inverTree(BiTree &t) 3 { 4 if(!t||(!t->lchild&&!t->rchild)) 5 { 6 return t; 7 } 8 swapNode(t); 9 inverTree(t->lchild); 10 inverTree(t->rchild); 11 12 } 13 void swapNode(BiTree &t) 14 { 15 if(!t||(!t->lchild&&!t->rchild)){return ;} 16 BiTree tmp =t->lchild; 17 t->lchild = t->rchild; 18 t->rchild = tmp; 19 }