微软面试百题010——二叉树节点最大距离
2016-08-14 14:10
435 查看
1.问题描述:
求二叉树结点中的最大距离(假设二叉树中节点都可以是双向的)2.思路:
先援引一段很牛逼的英文:ANSWER:
This is interesting... Also recursively(递归的), the longest distance between two nodes must be either from root to one leaf, or between two leafs. For the former case, it’s the tree height. For the latter case, it should be the sum of the heights of left and
right subtrees of the two leaves’ most least ancestor.
The first case is also the sum the heights of subtrees, just the height + 0.
根据上面的解答,我们会发现,二叉树中节点之间的最大的距离只有三种情况
第一种:经过根节点
第二种:全在左子树中
第三种:全在右子树中
所以说我们可以用递归的思路来求解这个问题:
3.代码:
#include"iostream" #include"cstdio" #include"cstdlib" #include"cstring" #define N 100 using namespace std; typedef struct node { struct node* left; struct node* right; int data; }point; class tree { public: tree() { memset(preorder,0,sizeof(preorder)); memset(midorder,0,sizeof(midorder)); root=NULL; num=ans=0; } void set() { cout<<"节点数目"<<endl; cin>>num; cout<<"前序遍历"<<endl; for(int i=0;i<num;i++) cin>>preorder[i]; cout<<"后序遍历"<<endl; for(int i=0;i<num;i++) cin>>midorder[i]; } void prebuild() { root=buildtree(preorder,midorder,num); } point* buildtree(int* pre,int* mid,int i) //i代表个数,构造函数类似于前序遍历的过程,只不过接住了中序遍历来进行划分 { if(i==0) return NULL; point* k=new point; k->data=pre[0]; k->left=k->right=NULL; int* p; int* q; int w=0; for(int i=0;i<num;i++) { if(mid[i]==pre[0]) { w=i; break; } } p=pre+1; q=mid; k->left=buildtree(p,q,w); p=pre+w+1; q=mid+w+1; k->right=buildtree(p,q,i-w-1); //w是左子树的个数,1是根节点的个数,i-w-1就是右子树的个数 return k; } void maxsituation(point* p,int& k) //返回目前最大的状态 { if(p==NULL) { k=-1; } else { int l,r; maxsituation(p->left,l); l++; maxsituation(p->right,r); r++; ans=max(max(ans,l),max(r,l+r)); //更新结果 if(p==root) return ; k=max(l,r); //利用引用,k代表当前节点伟根的子树可以贡献的最大的长度,这个长度的路径必定果根节点 } } void p() { maxsituation(root,ans); cout<<"结果是"<<ans<<endl; } point* returnroot() { return root; } void previsit(point* p) { if(p==NULL) return ; else { cout<<p->data<<' '; previsit(p->left); previsit(p->right); return ; } } private: int preorder ; int midorder ; point* root; int num; int ans; }; int main() { tree my; my.set(); my.prebuild(); my.previsit(my.returnroot()); my.p(); return 0; }
相关文章推荐
- 微软面试100道之11 求二叉树中节点的最大距离
- 微软面试100题之第11题 求二叉树中节点的最大距离
- 微软面试100题之11题:求二叉树中节点的最大距离
- 微软面试100题系列---求二叉树中节点的最大距离
- 微软等数据结构+算法面试100题(38)-- 二叉树中任意两个节点间的最大距离
- 面试经典(19)--求二叉树中节点的最大距离
- 面试100题:11.求二叉树中节点的最大距离
- July 微软面试100题 第11题 求二叉树的最大距离
- 【微软100题】求二叉树中节点的最大距离
- 微软100题(11) 二叉树中节点的最大距离
- 面试100题:11.求二叉树中节点的最大距离
- 求二叉树中结点的最大距离 【微软面试100题 第十一题】
- [面试备忘]求二叉树中节点最大距离 & 分层遍历二叉树
- 求二叉树中结点的最大距离 【微软面试100题 第十一题】
- 微软算法100题11 求二叉树中两节点之间的最大距离
- 9. 微软面试题:求二叉树中节点间最大距离
- 微软算法100道题-----求二叉树中节点的最大距离
- 【从零单排之微软面试100题系列】11之二叉树中结点的最大距离
- 【每日面试题】求一个二叉树中任意两个节点间的最大距离
- 微软100题第11题(求二叉树中节点的最大距离)