日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

基于visual Studio2013解决面试题之0601二叉树深度

發(fā)布時(shí)間:2025/5/22 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于visual Studio2013解决面试题之0601二叉树深度 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.



題目



解決代碼及點(diǎn)評(píng)

/*求二叉樹深度 */#include <iostream> #include <stack> using namespace std;template<class T> class BiTNode { public:T nValue;BiTNode<T> *pLChild;BiTNode<T> *pRChild; }; template<class T> class BiTree { public:BiTree();~BiTree();BiTNode<T> *Create();BiTNode<T> *getRoot();void InOrder(BiTNode<T> *p);void PostOrder(BiTNode<T> *p);void PreOrder(BiTNode<T> *p);void Visit(BiTNode<T> *p);int GetDeep(BiTNode<T> *p);int GetMaxLengh();private:BiTNode<T> *pRoot;int maxlengh; };template<class T> BiTree<T>::BiTree() {pRoot = new BiTNode<T>; }template<class T> BiTree<T>::~BiTree() {}template<class T> BiTNode<T> *BiTree<T>::Create() {T nValue;BiTNode<T> *nRoot;scanf_s("%d", &nValue);if (nValue == 0){nRoot = NULL;}else{nRoot = new BiTNode<T>;if (NULL == nRoot){printf("分配內(nèi)存失敗!\n");}else{nRoot->nValue = nValue;printf("請(qǐng)輸入%d結(jié)點(diǎn)的左子結(jié)點(diǎn):", nRoot->nValue);nRoot->pLChild = Create();printf("請(qǐng)輸入%d結(jié)點(diǎn)的右子結(jié)點(diǎn):", nRoot->nValue);nRoot->pRChild = Create();}}pRoot=nRoot;return nRoot; }template<class T> void BiTree<T>::Visit(BiTNode<T> *p){cout<< p->nValue; } template<class T> BiTNode<T> *BiTree<T>::getRoot() {return pRoot; } template<class T> void BiTree<T>::PreOrder(BiTNode<T> *pRoot) {if (pRoot==NULL){return ;} else{Visit(pRoot);PreOrder(pRoot->pLChild);PreOrder(pRoot->pRChild);} } template<class T> void BiTree<T>::InOrder(BiTNode<T> *pRoot) {if (pRoot==NULL){return ;} else{PreOrder(pRoot->pLChild);Visit(pRoot);PreOrder(pRoot->pRChild);} } template<class T> void BiTree<T>::PostOrder(BiTNode<T> *pRoot) {if (pRoot==NULL){return ;} else{PreOrder(pRoot->pLChild);PreOrder(pRoot->pRChild);Visit(pRoot);} }template<class T> int BiTree<T>::GetMaxLengh() //每一個(gè)節(jié)點(diǎn)左右深度相加比較最大值 {int maxlengh=0;int deep=0;int lengh=0;if (pRoot==NULL){return 0;}if (pRoot==NULL){return 0;} else{lengh = GetDeep(pRoot->pLChild)+GetDeep(pRoot->pRChild)+2;if (maxlengh<lengh){maxlengh=lengh;}}return maxlengh; }// 求樹的深度, 這道題的關(guān)鍵是理解樹的遞歸即可 template<class T> int BiTree<T>::GetDeep(BiTNode<T> *pRoot) {int deep=0;if (pRoot==NULL){return 0;}if (pRoot==NULL){return 0;} else // 樹的深度就是左子樹的深度 或者 由子樹的深度,取大的再加1即可{if (GetDeep(pRoot->pLChild)>GetDeep(pRoot->pRChild)){deep=GetDeep(pRoot->pLChild)+1;}else deep=GetDeep(pRoot->pRChild)+1;}return deep; }int main() {printf("請(qǐng)輸入根結(jié)點(diǎn)的值:");BiTree<int> pRoot ;pRoot.Create();printf("前序遍歷:");pRoot.PreOrder(pRoot.getRoot());cout<<endl;printf("中序遍歷:");pRoot.InOrder(pRoot.getRoot());cout<<endl;printf("后序遍歷:");pRoot.PostOrder(pRoot.getRoot());cout<<endl<<"深度"<<endl;cout<<pRoot.GetDeep(pRoot.getRoot());//cout<<endl<<"最長(zhǎng)距離";//cout<<pRoot.GetMaxLengh();system("pause");return 0; }

代碼下載及其運(yùn)行

代碼下載地址:http://download.csdn.net/detail/yincheng01/6704519

解壓密碼:c.itcast.cn


下載代碼并解壓后,用VC2013打開interview.sln,并設(shè)置對(duì)應(yīng)的啟動(dòng)項(xiàng)目后,點(diǎn)擊運(yùn)行即可,具體步驟如下:

1)設(shè)置啟動(dòng)項(xiàng)目:右鍵點(diǎn)擊解決方案,在彈出菜單中選擇“設(shè)置啟動(dòng)項(xiàng)目”


2)在下拉框中選擇相應(yīng)項(xiàng)目,項(xiàng)目名和博客編號(hào)一致

3)點(diǎn)擊“本地Windows調(diào)試器”運(yùn)行


程序運(yùn)行結(jié)果






轉(zhuǎn)載于:https://www.cnblogs.com/new0801/p/6177351.html

總結(jié)

以上是生活随笔為你收集整理的基于visual Studio2013解决面试题之0601二叉树深度的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。