日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

数据结构——交换左右子树

發布時間:2023/12/4 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构——交换左右子树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

遞歸——層次遍歷—交換左右子樹算法

思路:
與先序遞歸遍歷類似
1如果有子樹,交換這個節點的左右子樹(和交換兩個變量的值一樣)
2再遞歸這個節點的左子樹,右子樹;

#include<stdio.h> #include<bits/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)//?t2?ê÷μ??èDò′′?¨ {TElemType ch;scanf("%c",&ch);if(ch=='#')T=NULL;else {T=(BiNode*)malloc(sizeof(BiNode));if(!T)exit(-1);T->data=ch;CreateBiTree(T->lchild);CreateBiTree(T->rchild);} }void Exchange_lchild_rchild(BiTree &T)//????×óóòo¢×ó?áμ? {if(T!=NULL) if(T->lchild!=NULL||T->rchild!=NULL){BiTree temp;temp=T->lchild;T->lchild=T->rchild;T->rchild=temp;Exchange_lchild_rchild(T->lchild);Exchange_lchild_rchild(T->rchild);}}int BiTree_height1(BiTree T)//?óê÷μ?é??è??·¨1 {if(T==NULL)return 0;else{if(BiTree_height1(T->lchild)>BiTree_height1(T->rchild))return 1+BiTree_height1(T->lchild);elsereturn 1+BiTree_height1(T->rchild);}} void printNodeAtLevel(BiTree T,int level) { if(T==NULL||level<0) return; if(level==0) { 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(T==NULL)return;int totalLevel = BiTree_height1(T);for(int i = 0; i< totalLevel; i++){printNodeAtLevel(T, i);printf("\n");//′òó?íêò?2?£???DD} } int main(){BiTree T;printf("′′?¨ê÷ê?è?ê÷Tμ??èDòDòáD(???Dê1ó?#′ú±í???úμ?)\n");CreateBiTree(T);levelOrder(T);printf("????ê÷×óóò×óê÷??·¨\n");Exchange_lchild_rchild(T);levelOrder(T);}

總結

以上是生活随笔為你收集整理的数据结构——交换左右子树的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。