微软等数据结构算法面试微软等数据结构算法面试100题全部答案集锦
1把二元查找树转变成排序的双向链表题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。10614481216转换成双向链表46810121416。首先我们定义的二元查找树节点的数据结构如下:structBSTreeNodei
tm_
Valuevalueof
odeBSTreeNodem_pLeftleftchildof
odeBSTreeNodem_pRightrightchildof
odeANSWERThisisatraditio
alproblemthatca
besolvedusi
grecursio
Foreach
odeco
ectthedoubleli
kedlistscreatedfromlefta
drightchild
odetoformafulllistparamrootTheroot
odeofthetreeretur
Thehead
odeoftheco
vertedlistBSTreeNodetreeToLi
kedListBSTreeNoderootBSTreeNodeheadtailhelperheadtailrootretur
head
fvoidhelperBSTreeNodeheadBSTreeNodetailBSTreeNoderootBSTreeNodeltrhifrootNULLheadNULLtailNULLretur
helperheadltrootm_pLefthelperrhtailrootm_pRightifltNULLltm_pRightrootrootm_pLeftltelseheadrootifrhNULLrootm_pRightrhrhm_pLeftrootelsetailroot2设计包含mi
函数的栈。定义栈的数据结构,要求添加一个mi
函数,能够得到栈的最小元素。要求函数mi
、push以及pop的时间复杂度都是O1。ANSWERStackisaLIFOdatastructureWhe
someeleme
tispoppedfromthestackthestatuswillrecovertotheorigi
alstatusasbeforethateleme
twaspushedSoweca
recoverthemi
imumeleme
ttoostructMi
StackEleme
ti
tdatai
tmi
fstructMi
StackMi
StackEleme
tdatai
tsizei
ttopMi
StackMi
StackI
iti
tmaxSizeMi
StackstackstacksizemaxSizestackdataMi
StackEleme
tmallocsizeofMi
StackEleme
tmaxSizestacktop0retur
stackvoidMi
StackFreeMi
StackstackfreestackdatavoidMi
StackPushMi
Stackstacki
tdifstacktopstacksizeerror“outofstackspace”Mi
StackEleme
tpstackdatastacktoppdatadpmi
stacktop0dstackdatatop1ifpmi
dpmi
dtopi
tMi
StackPopMi
Stackstackifstacktop0error“stackisempty”retur
stackdatastacktopdatai
tMi
StackMi
Mi
Stackstackifstacktop0error“stackisempty”retur
stackdatastacktop1mi
3求子数组的最大和题目:
f输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为O
。例如输入的数组为123104725,和最大的子数组为310472,因此输出为该r