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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

基于visual Studio2013解决面试题之0210树的最远距离

發(fā)布時間:2025/3/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于visual Studio2013解决面试题之0210树的最远距离 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.



題目



解決代碼及點評

/*二叉樹的最大距離:將二叉樹看成圖,節(jié)點之間的距離為中間的線,求最大節(jié)點距離解決:如果左子樹和右子樹都不為空:最大距離 = 左子樹深度 + 右子樹深度 + 2*/#include <iostream> #include <stack> using namespace std;// 樹節(jié)點定義 template<class T> class BiTNode { public:T nValue; // 值BiTNode<T> *pLChild; // 左兒子節(jié)點BiTNode<T> *pRChild; // 右兒子節(jié)點 };// 二叉樹類 template<class T> class BiTree { public:BiTree(); // 構(gòu)造函數(shù)~BiTree(); // 析構(gòu)函數(shù)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() {}/* 通過鍵盤輸入創(chuàng)建樹 */ 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("請輸入%d結(jié)點的左子結(jié)點:", nRoot->nValue);nRoot->pLChild = Create();printf("請輸入%d結(jié)點的右子結(jié)點:", 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{/* 先序遍歷先訪問根節(jié)點,再遍歷左子樹也右子樹 */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() //每一個節(jié)點左右深度相加比較最大值 {int maxlengh = 0;int deep = 0;int lengh = 0;if (pRoot == NULL){return 0;}// 獲取左子樹和右子樹高度再相加就行l(wèi)engh = GetDeep(pRoot->pLChild) + GetDeep(pRoot->pRChild) + 2;if (maxlengh < lengh){maxlengh = lengh;}return maxlengh; }/*獲取樹的深度,方法為比較左子樹和右子樹深度,取比較大的那個值+1就行*/ template<class T> int BiTree<T>::GetDeep(BiTNode<T> *pRoot) {// 如果為root為NULL,則深度為0if (pRoot == NULL){return 0;}// 分別取左子樹深度和右子樹深度int ld = GetDeep(pRoot->pLChild);int rd = GetDeep(pRoot->pRChild);// 返回大的那個+1if (ld > rd)return ld + 1;return rd + 1; }int main() {printf("請輸入根結(jié)點的值:");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 << "最長距離";cout << pRoot.GetMaxLengh();system("pause");return 0; }

代碼下載及其運行

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

解壓密碼:c.itcast.cn


下載代碼并解壓后,用VC2013打開interview.sln,并設置對應的啟動項目后,點擊運行即可,具體步驟如下:

1)設置啟動項目:右鍵點擊解決方案,在彈出菜單中選擇“設置啟動項目”


2)在下拉框中選擇相應項目,項目名和博客編號一致

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


程序運行結(jié)果






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

總結(jié)

以上是生活随笔為你收集整理的基于visual Studio2013解决面试题之0210树的最远距离的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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