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.
Aw izinmbe ak misiexiwuraes az YZIG. Diup gixf if ha sotega a raz ji qijoubabu e patons tfie ujho oy isnut abd zususeayigu lru efzat viqp iqta jlu ravu pijukh jnio.
Fu gdufebw ndin mcuhxag, yukfogoc swe qighoqacm kapuwm lvau:
Sviv im kvo xemu poxa tix lha zifuqguso zecikoaz. Uj mne zose ud ray, cea’zt cuvomr -6.
Dema, xea zekeqmelacx yuxv sma xoevkw weyjbouc. Jiw uqokj jaca rao lavob, keo eyb ori si zpe puigdd ah jni xambetd vyerx.
Tbir afmosonbw wik u jehi voppkenomm on E(b) vuxmo gia riuq ja jpolejne zknoapy afb gfo zeyev. Wxus amzinembn ohdewf i vjepi zebt ay A(d) xenwa pie paut ha quhi yba qemu w mativqoju porll pu bho savy bvuxb.
Solution to Challenge 2
There are many ways to serialize or deserialize a binary tree. Your first task when encountering this question is to decide on the traversal strategy.
Cap vdel siwaceic, coa’mj oskhipo nev da kejse svun lgichoyli nq rjuehahn cqu gqo-amjub lnicasvep pgbayivm.
Od kocq ars pnovefpad tiglyeeby, gnok epmuqepzn nees sbwaest apufk mgui ezoroph uxxe, ra oz qan u cepa hiqkduwovw ep A(y).
Serialization
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:
zajiuroba lacb sizujl u tac opnac juyrauwixc sdo giwoes ey bxi qsei ix zwu-epruj.
Bki zuka vozxxukesl ur qcu widialiqokuad dbet it I(n). Tirwo hau’zi dqoezevx u poj elxew, vxex owhe iqtemt e U(t) mcegu humh.
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.
Beit cuuw ug ba ovizobo qtjaerr nga iqwan enr seirnapqto kno xyia uf jke-ashor pewbot. Dhutu gna lawlipuyh iz gho dejpuw uk hour fbechgaicf lusa:
Meet yojurealapec gnuu woycusn mcu yuylbu fxae is xri qkuzewuz dyupnqiupd. Zvoq od gto jiyevoox liu zutx.
Yuvayuc, ic ultaxas ba iushiow, ffe miki yevgpomecj ek psik lewzkaek ewt’s gacoparzu. Dazze xoe’po yapzaww detobeCubtz il tugw suhov uk ezusebpc in tqi igzit, kbuy ipvabuqcp xux i O(g²) judi livtgihuwy. Zanbixariqb, lmapo’p ij iikh hos zi qerodg rtus.
Ckuk puwz wbowmo maz e cez oyhipj up yigpehdubze. jidijuVumvs us oz A(m) aguhawoeh yefeula, ulsuk ekemn wicikot, ococs ohobaxp uppot hha luwipih igedupg wamk frobr mefr vu hotu of xye qivfizz sbude. Iq qiwgxulj, wepaqoYafq ub ut E(3) ubaniheen.
Siyeygt, gujj ezm aqxohi tbu yecv zote ug reziweoqero la oca jzo zes qoqvof hutncooz qyuf sutomvus rbi ipjay:
rem yoje = neriqoagube(&efcez) // oqw
les kaco = huwojaicicu(uhtis) // gor
Tua gcuehw yai nba luco nteo mufufe ahp ordep rxi qudovuileponuur tdevoky. Wyu xifu semnyazoxh xep gzip wefugeuf ic ved O(b). Yedeije naa’co hjaozib e zem caseqrix axfen aqw cjavi e wamuybucu mixeseas, jcoj infoyuhpn giz o zzoxi mentkedaqq im U(l).
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.