微软面试一百道题目精选.docx
- 文档编号:2139949
- 上传时间:2022-10-27
- 格式:DOCX
- 页数:50
- 大小:62.50KB
微软面试一百道题目精选.docx
《微软面试一百道题目精选.docx》由会员分享,可在线阅读,更多相关《微软面试一百道题目精选.docx(50页珍藏版)》请在冰豆网上搜索。
微软面试一百道题目精选
第9题
判断整数序列是不是二元查找树的后序遍历结果
题目:
输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。
如果是返回true,否则返回false。
例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果:
8
/\
610
/\/\
57911
因此返回true。
如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。
ANSWER:
Thisisaninterestingone.Thereisatraditionalquestionthatrequiresthebinarytreetobere-constructedfrommid/post/preorderresults.Thisseemssimilar.Fortheproblemsrelatedto(binary)trees,recursionisthefirstchoice.
Inthisproblem,weknowinpost-orderresults,thelastnumbershouldbetheroot.SowehaveknowntherootoftheBSTis8intheexample.Sowecansplitthearraybytheroot.
intisPostorderResult(inta[],intn){
returnhelper(a,0,n-1);
}
inthelper(inta[],ints,inte){
if(e==s)return1;
inti=e-1;
while(a[e]>a[i]&&i>=s)i--;
if(!
helper(a,i+1,e-1))
return0;
intk=l;
while(a[e]=s)i--;
returnhelper(a,s,l);
}
第10题
翻转句子中单词的顺序。
题目:
输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。
句子中单词以空格符隔开。
为简单起见,标点符号和普通字母一样处理。
例如输入“Iamastudent.”,则输出“student.aamI”。
Answer:
Alreadydonethis.Skipped.
第11题
求二叉树中节点的最大距离...
如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,
我们姑且定义"距离"为两节点之间边的个数。
写一个程序,
求一棵二叉树中相距最远的两个节点之间的距离。
ANSWER:
Thisisinteresting...Alsorecursively,thelongestdistancebetweentwonodesmustbeeitherfromroottooneleaf,orbetweentwoleafs.Fortheformercase,it’sthetreeheight.Forthelattercase,itshouldbethesumoftheheightsofleftandrightsubtreesofthetwoleaves’mostleastancestor.
Thefirstcaseisalsothesumtheheightsofsubtrees,justtheheight+0.
intmaxDistance(Node*root){
intdepth;
returnhelper(root,depth);
}
inthelper(Node*root,int&depth){
if(root==NULL){
depth=0;return0;
}
intld,rd;
intmaxleft=helper(root->left,ld);
intmaxright=helper(root->right,rd);
depth=max(ld,rd)+1;
returnmax(maxleft,max(maxright,ld+rd));
}
第12题
题目:
求1+2+…+n,
要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句
(A?
B:
C)。
ANSWER:
1+..+n=n*(n+1)/2=(n^2+n)/2
itiseasytogetx/2,sotheproblemistogetn^2
thoughnoif/elseisallowed,wecaneasillygoaroundusingshort-pass.
usingmacrotomakeitfancier:
#define T(X,Y,i)(Y&(1<
intfoo(intn){
intr=n;
T(r,n,0);T(r,n,1);T(r,n,2);…T(r,n,31);
returnr>>1;
}
第13题:
题目:
输入一个单向链表,输出该链表中倒数第k个结点。
链表的倒数第0个结点为链表的尾指针。
链表结点定义如下:
structListNode
{
intm_nKey;
ListNode*m_pNext;
};
Answer:
Twoways.1:
recordthelengthofthelinkedlist,thengon-ksteps.2:
usetwocursors.
Timecomplexitiesareexactlythesame.
Node*lastK(Node*head,intk){
if(k<0)error(“k<0”);
Node*p=head,*pk=head;
for(;k>0;k--){
if(pk->next!
=NULL)pk=pk->next;
elsereturnNULL;
}
while(pk->next!
=NULL){
p=p->next,pk=pk->next;
}
returnp;
}
第14题:
题目:
输入一个已经按升序排序过的数组和一个数字,
在数组中查找两个数,使得它们的和正好是输入的那个数字。
要求时间复杂度是O(n)。
如果有多对数字的和等于输入的数字,输出任意一对即可。
例如输入数组1、2、4、7、11、15和数字15。
由于4+11=15,因此输出4和11。
ANSWER:
Usetwocursors.Oneatfrontandtheotherattheend.Keeptrackofthesumbymovingthecursors.
voidfind2Number(inta[],intn,intdest){
int*f=a,*e=a+n-1;
intsum=*f+*e;
while(sum!
=dest&&f if(sum elsesum=*(--e); } if(sum==dest)printf(“%d,%d\n”,*f,*e); } 第15题: 题目: 输入一颗二元查找树,将该树转换为它的镜像, 即在转换后的二元查找树中,左子树的结点都大于右子树的结点。 用递归和循环两种方法完成树的镜像转换。 例如输入: 8 /\ 610 /\/\ 57911 输出: 8 /\ 106 /\/\ 11975 定义二元查找树的结点为: structBSTreeNode//anodeinthebinarysearchtree(BST) { intm_nValue;//valueofnode BSTreeNode*m_pLeft;//leftchildofnode BSTreeNode*m_pRight;//rightchildofnode }; ANSWER: Thisisthebasicapplicationofrecursion. PS: Idon’tlikethem_xxnamingconvension. voidswap(Node**l,Node**r){ Node*p=*l; *l=*r; *r=p; } voidmirror(Node*root){ if(root==NULL)return; swap(&(root->left),&(root->right)); mirror(root->left); mirror(root->right); } voidmirrorIteratively(Node*root){ if(root==NULL)return; stack buf.push(root); while(! stack.empty()){ Node*n=stack.pop(); swap(&(root->left),&(root->right)); if(root->left! =NULL)buf.push(root->left); if(root->right! =NULL)buf.push(root->right); } } 第16题: 题目(微软): 输入一颗二元树,从上往下按层打印树的每个结点,同一层中按照从左往右的顺序打印。 例如输入 8 /\ 610 /\/\ 57911 输出861057911。 ANSWER: Thenodesinthelevelsareprintedinthesimilarmannertheirparentswereprinted.SoitshouldbeanFIFOqueuetoholdthelevel.Ireallydon’trememberthefunctionnameofthestlqueue,soIwillwriteitinJava... voidprintByLevel(Noderoot){ Nodesentinel=newNode(); LinkedList q.addFirst(root);q.addFirst(sentinel); while(! q.isEmpty()){
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 微软 面试 一百 题目 精选