基于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语义化命名方式及常用命
- 下一篇: IE6/IE7下:inline-bloc