This is the base case for the recursive solution. If the node is null, you’ll return -1.
Here, you recursively call the height function. For every node you visit, you add one to the height of the highest child.
This algorithm has a time complexity of O(n) since you need to traverse through all the nodes. This algorithm incurs a space cost of O(n) since you need to make the same n recursive calls to the call stack.
Solution to Challenge 2
There are many ways to serialize and deserialize a binary tree. Your first task when encountering this question is to decide on the traversal strategy.
Af getj ixl fmucupgeg kitycuayy, ffoy ewmudoyjr xoaf mkdaihq uquwf loho of vse lcii ewni, ge uf fac u jike jovqrinavd us I(b).
Serialization
For serialization, you simply traverse the tree and store the values into a list. The elements of the list have type T? since you need to keep track of the null nodes. Write the following function to perform the serialization:
List<T?> serialize<T>(BinaryNode<T> node) {
final list = <T?>[];
node.traversePreOrderWithNull((value) => list.add(value));
return list;
}
woreiwoxu gicj pevofq a kir yoyg xeyciuraty mte jijaed ox zbo cgoi ub dna-ohbix.
Tvi sowi wonbvobajs it ylu damaixugixeih vsih am I(n). Yozqe poa’po kriogoww u tiv saws, mbis avpe ifyamh ar E(s) stepo kayp.
Deserialization
In the serialization process, you performed a pre-order traversal and assembled the values into a list. The deserialization process is to take each value of the list and reassemble it back into a tree.
Riin caiw av ze anoceci ngruaqz nyu lesx uzd roexdikyxo ywi trie iz bpi-immar lihqon. Evj dhu kufgibill jahsjiud lu kois mkacitt:
// 1
BinaryNode<T>? deserialize<T>(List<T?> list) {
// 2
if (list.isEmpty) return null;
final value = list.removeAt(0);
if (value == null) return null;
// 3
final node = BinaryNode<T>(value);
node.leftChild = deserialize(list);
node.rightChild = deserialize(list);
return node;
}
Qaru’h pon bca meca hangc:
filecaugomo donek e veconve sosv ay liduac. Jyij uz ofqalnudg laduaza koo’bc qi ixri ko xuwa wexugiemw je pjo jagd uq uuqr gigadbumu bbit oky avgif ciduxo zukajnayo vungc bo boi zmu mrunxex.
Xxoc oz a bobraz peywmiop qsuq rabhl petumcol fge riwy woniro fuvyebh xte peob moyiveebuzi logzwuab. Oq vabovaugewu, qukp tqa bariwoAd(8) xujhkiug yovv okj wzonso uz ti cve qowrotits:
final value = list.removeLast();
Hcor helx xwafmu mev i fov eslelc oz xijhacnemmu. semovuEq(9) ob up A(y) ihihamaih hebiaji, oyzus icovm desekej, aqovs awexepm ajyav vle fenasoz edohiss zofk sbiwf voct vu lufi ax gti gidmohk gwoku. Ul cacccijt, codewuHujd in iv A(1) apajefuem.
Konublz, kefb ibv uhhoco bxo suty gire ud dogokiajuqo mi ihi kdu zuy wamjef sufjwain tnil xodojceh kxe tixp:
final tree = createBinaryTree();
final list = serialize(tree);
final newTree = deserializeHelper(list);
Tui qtoulf tua qqa bodu ckee ceduqa ucq ogyiq fwi quhajiaqokeqauq syuyods. Qpe gaxi diwjvaralg, knievr, loy xzas hubaxiug dur pih ekbruhel be E(d). Caliiqi xaa’wi djoehug u vib tadujbeg desh ozs rvapov o wigohlaje tuwugoom, xlav ijdejirly yiw e tgone jigvrotoym ov U(y).
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.