4.6Designanalgorithmandwritecodetofindthefirstcommonancestoroftwonodesinabinarytree.Avoidstoringadditionalnodesinadatastructure.NOTE:Thisisnotnecessarilyabinarysearchtree.BTNodefind(Noden,Nodea,Nodeb)
{
if(n==a||n==b)
{
returnn;
}
intnodeMatch=nodeMatch(a...
分类:
其他好文 时间:
2014-11-27 08:02:42
阅读次数:
180
3.3Imaginea(literal)stackofplates.Ifthestackgetstoohigh,itmighttopple.Therefore,inreallife,wewouldlikelystartanewstackwhenthepreviousstackexceedssomethreshold.ImplementadatastructureSetOfStacksthatmimicsthis.SetOfStacksshouldbecomposedofseveralstacks,andsho..
分类:
其他好文 时间:
2014-11-25 02:01:22
阅读次数:
201
3.4IntheclassicproblemoftheTowersofHanoi,youhave3rodsandNdisksofdifferentsizeswhichcanslideontoanytower.Thepuzzlestartswithdiskssortedinascendingorderofsizefromtoptobottom(e.g.,eachdisksitsontopofanevenlargerone).Youhavethefollowingconstraints:(A)Onlyonedis..
分类:
其他好文 时间:
2014-11-25 02:00:22
阅读次数:
115
3.1Describehowyoucoulduseasinglearraytoimplementthreestacks.3stacks?separatethearrayinto3sections.Use3index.push(intstack,Tt)
{
validateStackNum(stack);//Validate(stack>=0&&stack<2);
intpos=stackPos[stack];
check(pos+1);//ifexceeds,enlargeth..
分类:
其他好文 时间:
2014-11-24 12:09:27
阅读次数:
161
3.2Howwouldyoudesignastackwhich,inadditiontopushandpop,alsohasafunctionminwhichreturnstheminimumelement?Push,popandminshouldalloperateinO(1)time.Useanotherstackmaintainingmin.
push(Tt)
{
mainStack.push(t);
TcurMin=minStack.peek();
if(curMin==null||t<curM..
分类:
其他好文 时间:
2014-11-24 12:08:38
阅读次数:
183
2.3Implementanalgorithmtodeleteanodeinthemiddleofasinglelinkedlist,givenonlyaccesstothatnode.EXAMPLEInput:thenode‘c’fromthelinkedlista->b->c->d->eResult:nothingisreturned,butthenewlinkedlistlookslikea->b->d->eItseemscannotdirecltydele..
分类:
其他好文 时间:
2014-11-24 08:45:39
阅读次数:
106
2.2Implementanalgorithmtofindthenthtolastelementofasinglylinkedlist.//Assumetheinputlisthasnocircle.
//O(n)
NodefindNthNodeToLast(Noderoot,intn)
{
//Findsize
Noden=root;
intsize=0;
while(n!=null)
{
size++;
n=n.next;
}
if(n>size)
returnnull;
intnFromThe..
分类:
其他好文 时间:
2014-11-24 08:44:50
阅读次数:
126
2.5Givenacircularlinkedlist,implementanalgorithmwhichreturnsnodeatthebeginningoftheloop.DEFINITIONCircularlinkedlist:A(corrupt)linkedlistinwhichanode’snextpointerpointstoanearliernode,soastomakealoopinthelinkedlist.EXAMPLEinput:A->B->C->D->E-&g..
分类:
其他好文 时间:
2014-11-24 08:44:00
阅读次数:
122
2.4Youhavetwonumbersrepresentedbyalinkedlist,whereeachnodecontainsasingledigit.Thedigitsarestoredinreverseorder,suchthatthe1’sdigitisattheheadofthelist.Writeafunctionthataddsthetwonumbersandreturnsthesumasalinkedlist.EXAMPLEInput:(3->1->5)+(5->9-&..
分类:
其他好文 时间:
2014-11-24 08:43:11
阅读次数:
120
实现一个算法来删除单链表中间的一个结点,只给出指向那个结点的指针。...
分类:
编程语言 时间:
2014-11-14 19:48:54
阅读次数:
199