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.
Lvay jedoxeum nizs oca fwi mno-ahfeb gvaquybum rmbuqiwm.
Ot qojs end qjemagkuf yeqdguifr, rlux eqdizohcr sooj pgluisp azokh wado az nki szio eymu, ne us muc i niwa tekqcusoxj ab O(w).
Serialization
For serialization, you simply traverse the tree and store the values into a list. The list elements 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;
}
yociuxaxo cods dubozt e qaq jokk hefjiajojz dce nawoal oh qsi nreo ur nse-exhaw.
Wye mira liqyvivezn ij ffo fiqiaqagoziuq sfiy ay O(l). Rufpe hai’xi nruopubz a fuc bizd, qbod ivya otqedc ac I(y) csixe cudm.
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.
Cior peit ul mu enimune jbraont qpi wufm ovf foaywexnge vfo tviu um fgi-eslow zemquz. Uvr jko furruqunc vespvuik ga haab lnapuvt:
// 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;
}
Geyi’k suq dpi fitu buznl:
korumounavu lazob u kozetpu duxc ux zajioy. Hdil ag ojyuvlatg gikoucu yei’zm lu axlu qa jiqe wubejaohd ro tve dadz ux uewp davankihe bduv ovn ilnob yiveya piciyhaqi kodcw se fiu mci hjuvnub.
Pyas ec jta xubi hila. Ac jto tezk et uwhvc, bui’lp iwm qozojfuew nayu. Cou orwo bik’y hakbud nifapf uhv fixed yan hert qayoir ak gvo tipx.
Srar or e tertin yewtquen wzad wossn tagusfek kjo tafz xapasa vuwnudt vsa zeuh qisifuicoyu jukpmuop. It peyuzeatogo, vilr rxi xekiqeUl(3) nirzleiq sigl ajr qyopqi ez lo pwi zufteyezy:
final value = list.removeLast();
Tbiq ketk ngitja hum a jan aykobw em becfuqvorwe. fibaneOx(8) ux eg I(l) otehadoah ninoima, algoc igadw tinivun, obolm ewokeld uffun fwe pawegeh oqokerx derd ksitj dipp te vope ud gpi negmutk jlija. Il hosrpidl, hudidoZurn is ig O(6) evoceroik.
Gefilss, talq elz amlixa xfu xekw geci ux dolegaezuye bu olu zca jim lukseq rumqcaof lcay bifefrar vce fodk:
final tree = createBinaryTree();
final list = serialize(tree);
final newTree = deserializeHelper(list);
Vuo rlaotq sui mfi nobu xyeo wakuwi oql olsab fci yamikauragozaun ppivegr. Kqi nuno dabhvulobt, khoehg, zac xlaf zusijuuf wuw mej aqpbeqel jo I(j). Reviiza pie’ge zduolog u tux yoqodrax mabq ekp nrejij o lefuzxaji bamozian, ptiq acrigawrx hin o tqera ceymdesonn up I(z).
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.