基于visual Studio2013解决面试题之0601二叉树深度
生活随笔
收集整理的這篇文章主要介紹了
基于visual Studio2013解决面试题之0601二叉树深度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
解決代碼及點評
/*求二叉樹深度 */#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;}if (pRoot==NULL){return 0;} else{lengh = GetDeep(pRoot->pLChild)+GetDeep(pRoot->pRChild)+2;if (maxlengh<lengh){maxlengh=lengh;}}return maxlengh; }// 求樹的深度, 這道題的關鍵是理解樹的遞歸即可 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("請輸入根結點的值:");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/6177351.html
總結
以上是生活随笔為你收集整理的基于visual Studio2013解决面试题之0601二叉树深度的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 你应该了解的CSS语义化命名方式及常用命
- 下一篇: 搭完环境,最后登录时提示“与数据库连接失