Maximum Depth of N-ary Tree

🏠 ⬅️ ➡️

Given a n-ary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

Example 1:

Input: root = [1,null,3,2,4,null,5,6] Output: 3

Example 2:

Input: 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] Output: 5

Constraints:

  • The total number of nodes is in the range [0, 104].
  • The depth of the n-ary tree is less than or equal to 1000.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    type :: Node
        integer :: val
        type(Node), pointer :: left(:) => null()
        type(Node), pointer :: right(:) => null()
    end type

    type(Node), target :: root
    type(Node), pointer :: curr
    integer :: i, j

    ! Example 1
    allocate(root%left(1))
    allocate(root%right(2))
    root%val = 1
    root%left(1)%val = 3
    root%right(1)%val = 2
    root%right(2)%val = 4

    print '(I0)', maxDepth(root)

    ! Example 2
    deallocate(root%left)
    deallocate(root%right)
    allocate(root%left(2))
    allocate(root%right(3))
    root%val = 1
    root%left(1)%val = 5
    root%left(2)%val = 6
    root%right(1)%val = 7
    root%right(2)%val = 8
    root%right(3)%val = 9
    root%right(3)%left(1)%val = 10
    root%right(3)%right(1)%val = 11
    root%right(3)%right(2)%val = 12
    root%right(3)%right(2)%left(1)%val = 13
    root%right(3)%right(2)%right(1)%val = 14

    print '(I0)', maxDepth(root)

contains

    function maxDepth(root) result(depth)
        implicit none
        class(Node), intent(in) :: root
        integer :: depth
        type(Node), pointer :: curr
        integer :: i

        depth = 0
        curr => root
        do while (associated(curr))
            if (associated(curr%left)) then
                depth = max(depth, maxDepth(curr%left))
            end if
            if (associated(curr%right)) then
                depth = max(depth, maxDepth(curr%right))
            end if
            curr => curr%left
        end do
    end function

end program
Compiled
Executed
Correct
! This is a Fortran implementation of the problem.
! The program takes a n-ary tree as input and outputs its maximum depth.

program max_depth

implicit none

! Declare variables
integer :: n, i, j, k, l, m, n_nodes, max_depth
integer, allocatable :: nodes(:)

! Read the number of nodes in the tree
read (*,*) n_nodes

! Allocate memory for the nodes
allocate (nodes(n_nodes))

! Read the nodes of the tree
do i = 1, n_nodes
    read (*,*) nodes(i)
end do

! Initialize the maximum depth
max_depth = 0

! Iterate over the nodes
do i = 1, n_nodes
    ! If the node is not null, increment the maximum depth
    if (nodes(i) /= 0) then
        max_depth = max_depth + 1
    end if
end do

! Output the maximum depth
write (*,*) max_depth

end program max_depth
🌐 Data from online sources
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children if children is not None else []

def max_depth(root):
    if root is None:
        return 0
    max_depth = 0
    for child in root.children:
        max_depth = max(max_depth, max_depth(child))
    return max_depth + 1

The algorithm used for all the language implementations is Depth First Traversal. The base case checks if the current root is null or not. If it is null, the function returns 0. Then, we iterate through each child node in the children list and calculate the maximum depth from each child node by making a recursive call. The maximum depth from a child node is added to the current depth which is initialized to 0. Finally, we return the maximum depth + 1 to account for the current node.

🌐 Data from online sources
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};

int maxDepth(Node* root) {
    if (!root) return 0;
    int max_depth = 0;
    for (Node* child : root->children) {
        max_depth = max(max_depth, maxDepth(child));
    }
    return max_depth + 1;
}

The algorithm used for all the language implementations is Depth First Traversal. The base case checks if the current root is null or not. If it is null, the function returns 0. Then, we iterate through each child node in the children list and calculate the maximum depth from each child node by making a recursive call. The maximum depth from a child node is added to the current depth which is initialized to 0. Finally, we return the maximum depth + 1 to account for the current node.