133x
Filetype PDF
File size 0.07 MB
Source: courses.csail.mit.edu
File: Hacking A Google Interview Practice Questions Person B
HackingaGoogleInterviewPracticeQuestions– PersonB Question:BinarySearchTreeValidity Writeafunctiontodeterminewhetheragivenbinarytreeofdistinctintegersisa validbinarysearchtree.Assumethateachnodecontainsapointertoitsleftchild,a pointertoitsrightchild,andaninteger,butnotapointertoitsparent.Youmayuse anylanguageyoulike. GoodAnswer:Notethatit'snotenoughtowritearecursivefunctionthatjustchecks iftheleftandrightnodesofeachnodearelessthanandgreaterthanthecurrent node(andcallsthatrecursively).Youneedtomakesurethatallthenodesofthe subtreestartingatyourcurrentnodearewithinthevalidrangeofvaluesallowedby thecurrentnode'sancestors.Thereforeyoucansolvethisrecursivelybywritinga helperfunctionthatacceptsacurrentnode,thesmallestallowedvalue,andthe largestallowedvalueforthatsubtree.Anexampleofthisisthefollowing(inJava): boolean isValid(Node root) { ...
Filetype PDF | Posted on 05 Feb 2023 | 2 years ago