基于visual Studio2013解决面试题之0210树的最远距离
生活随笔
收集整理的這篇文章主要介紹了
基于visual Studio2013解决面试题之0210树的最远距离
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
解決代碼及點評
/*二叉樹的最大距離:將二叉樹看成圖,節點之間的距離為中間的線,求最大節點距離解決:如果左子樹和右子樹都不為空:最大距離 = 左子樹深度 + 右子樹深度 + 2*/#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");}else{nRoot->nValue = nValue;printf("請輸入%d結點的左子結點:", nRoot->nValue);nRoot->pLChild = Create();printf("請輸入%d結點的右子結點:", 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() //每一個節點左右深度相加比較最大值 {int maxlengh = 0;int deep = 0;int lengh = 0;if (pRoot == NULL){return 0;}// 獲取左子樹和右子樹高度再相加就行lengh = 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("請輸入根結點的值:");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調試器”運行
程序運行結果
轉載于:https://www.cnblogs.com/new0801/p/6177379.html
總結
以上是生活随笔為你收集整理的基于visual Studio2013解决面试题之0210树的最远距离的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL 16进制数转化10进制
- 下一篇: 三种banner基础形态