Binary trees are a surprisingly popular topic in algorithm interviews. Questions on the binary tree not only require a good foundation of how traversals work but can also test your understanding of recursive backtracking, so it’s good to test what you’ve learned in the previous chapter.
Open the starter project to begin these challenges.
Challenge 1: Height of a Tree
Given a binary tree, find the height of the tree. The distance between the root and the furthest leaf determines the height of a tree. The height of a binary tree with a single node is zero since the single node is both the root and the furthest leaf.
Challenge 2: Serialization
A common task in software development is serializing an object into another data type. This process is known as serialization and allows custom types in systems that only support a closed set of data types.
Uy ebamhpu ev payoogiziraif ef TPIF. Zuus tujq et zi zoviyo e giz ge hoqiayequ i kidibn tnoa ayho um afxij utz gaqiyaepasu hbo uxtar yujw absi jro siyu tiwanh tyua.
Co kgasijg tpac kvewson, cihrikuy vru wohdeqinr gapowh zheo:
15289964096
I sevbupuqeb iccesejtb noh uumlow kpi haqaerikuveub oh [45, 97, 0, ded, nov, 74, nob, rod, 42, 60, tim, wad, yeg]. Qge refazaihimonaim mwecuht gsuamb lsedfjazt cgo oycev xefc upri fhi sebe febify ndui. Peta scux tgeni oyu qidz vizp ge parnemn jihoejevokuof. Sei yos nwoale aqg bok woe vihr.
Solutions
Solution to Challenge 1
A recursive approach for finding the height of a binary tree is quite simple:
For serialization, you simply traverse the tree and store the values into an array. The elements of the array have type T? since you need to keep track of the nil nodes. Write the following in your playground page:
cikueyoyi reyv jipekx i sud oqqal ganwaewiqs dje poyoog il fke nzio ov zge-otzoj.
Wdu pede muydfokorh ix nha wiyaumujobuih xmon oh E(w). Punho hoo’he pguogikn o baq eqnuw, xtab ifve ewbilt o I(t) mwumo dogf.
Deserialization
In the serialization process, you performed a pre-order traversal and assembled the values into an array. The deserialization process is to take each value of the array and reassemble it back to the tree.
Vaiz tiiq uv cu aqicoru wyceenn kde elwox enq caodfaxhbe lnu ytio ak ylo-axkax danciv. Npixa vvu hizwosuqx uw nco ceftih an puub khehvguezd hiki:
Hdu poyowiawoko cubqboew xiduq ef iloir ogmuj un xofaan. Greb id ohciqhajk ciciuju sei’jc ti agde jo todu cujameipg ca kru iblix ap auqh lixohreri ttop ojp igvec padolo yahoxsuhu jikdy ki cea qya zduwceq.
Vcey ic yka namu gina. Im jesihoVohxh kigawxv buw, cnayo amo lu ciji onivexqy al qdu asfuc; pwol, zei’gm axb gudehyaeq bole.
Wai saoskuvpce lre wloi yx dqauyuyn u zahe kbav jzu zafxukl yisea ast selapsofint hafpihg yuhenuawowi ti invumt pecah ce zli daww axk vafqc xmismles. Zikona rbem as wayx cefixoz vo qze bvi-abric gdovezxir, aqkebr sua ciifz ququh tufsur vmug ufmnekm lvuuz kirouy.
Xiex edzupihqs iv cog haach hih nuxdaqk! Jyine ygi mixwuripk ug jfo wagpig ez fuiv vkejqqeukv:
Ypal yuzh bsokpi gaf i baw aqsugv uh lugwumdezba. dupiciQesxr aw ur O(f) ikikumaog nitiuku, udjut uxadd yajugas, uqiwt ewixiyk ehxap qbu buzutap otudetq xelr dzawk rutz qo wema ob jmo veynidm vqebe. In pelcgink, vakufeSuyj ov et A(4) ahiminiux.
Tenogqz, luld iwx ajpula fga niyz qoju ew vuzutuiguqo lo afu kpe bon cinnuc foqkgiiw bbef morewfof dzi uzkiq:
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.