Loading content...
Given the root of an N-ary tree, determine the total height of the tree. The height of an N-ary tree is defined as the count of nodes along the longest path from the root node to any leaf node in the tree.
An N-ary tree is a rooted tree structure where each node can have an arbitrary number of children (zero or more). Unlike binary trees that are limited to at most two children per node, N-ary trees provide a more flexible hierarchical structure.
Tree Serialization Format:
The input tree is provided using level-order traversal serialization. In this format, groups of children for each node are separated by null values. Each null acts as a delimiter indicating the end of one node's children list and the beginning of the next node's children at the same level.
For example, the serialization [1, null, 3, 2, 4, null, 5, 6] represents:
If the tree is empty (root is null), return 0.
root = [1,null,3,2,4,null,5,6]3The tree has three levels. The root node (1) is at level 1. Its children (3, 2, 4) are at level 2. The children of node 3 (nodes 5 and 6) are at level 3. The longest path from root to leaf contains 3 nodes.
root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]5This more complex tree has 5 levels. The deepest path from the root reaches nodes at level 5, making the height of the tree 5.
root = [1,null,2,3,4]2The root node (1) has three children (2, 3, 4). None of these children have any further descendants, so the tree has exactly 2 levels.
Constraints