wordpress主题域名授权,网站排名的优化,优酷专门给马天宇做的网站,做网站建设很赚钱吗递归——层次遍历—交换左右子树算法
思路#xff1a; 与先序递归遍历类似 1如果有子树#xff0c;交换这个节点的左右子树#xff08;和交换两个变量的值一样#xff09; 2再递归这个节点的左子树#xff0c;右子树#xff1b;
#includestdio.h
#includeb…递归——层次遍历—交换左右子树算法
思路 与先序递归遍历类似 1如果有子树交换这个节点的左右子树和交换两个变量的值一样 2再递归这个节点的左子树右子树
#includestdio.h
#includebits/stdc.h
typedef char TElemType;
typedef int status;
typedef struct BiNode
{TElemType data;struct BiNode *lchild;struct BiNode *rchild;
}BiNode,*BiTree;
void CreateBiTree(BiTree T)//¶þ²æÊ÷µÄÏÈÐò´´½¨
{TElemType ch;scanf(%c,ch);if(ch#)TNULL;else {T(BiNode*)malloc(sizeof(BiNode));if(!T)exit(-1);T-datach;CreateBiTree(T-lchild);CreateBiTree(T-rchild);}
}void Exchange_lchild_rchild(BiTree T)//½»»»×óÓÒº¢×Ó½áµã
{if(T!NULL) if(T-lchild!NULL||T-rchild!NULL){BiTree temp;tempT-lchild;T-lchildT-rchild;T-rchildtemp;Exchange_lchild_rchild(T-lchild);Exchange_lchild_rchild(T-rchild);}}int BiTree_height1(BiTree T)//ÇóÊ÷µÄÉî¶ÈËã·¨1
{if(TNULL)return 0;else{if(BiTree_height1(T-lchild)BiTree_height1(T-rchild))return 1BiTree_height1(T-lchild);elsereturn 1BiTree_height1(T-rchild);}}
void printNodeAtLevel(BiTree T,int level)
{ if(TNULL||level0) return; if(level0) { printf(%c ,T-data);return; } // ×ó×ÓÊ÷µÄ level - 1 ¼¶ printNodeAtLevel(T-lchild,level-1); // ÓÒ×ÓÊ÷µÄ level - 1 ¼¶ printNodeAtLevel(T-rchild,level-1);
}void levelOrder(const BiTree T)
{if(TNULL)return;int totalLevel BiTree_height1(T);for(int i 0; i totalLevel; i){printNodeAtLevel(T, i);printf(\n);//´òÓ¡ÍêÒ»²ã£¬»»ÐÐ}
} int main(){BiTree T;printf(´´½¨Ê÷ÊäÈëÊ÷TµÄÏÈÐòÐòÁÐ(ÆäÖÐʹÓÃ#´ú±í¿Õ½Úµã)\n);CreateBiTree(T);levelOrder(T);printf(½»»»Ê÷×óÓÒ×ÓÊ÷Ëã·¨\n);Exchange_lchild_rchild(T);levelOrder(T);}