Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-116

Tree Depth from Heads

Problem

Return the maximum depth of a dependency tree given heads, where heads[i] is the parent index of token i and -1 marks a root.

A single root has depth 0, and an empty head list has depth 0.

Hint

Walk each token up to the root and keep the largest number of hops.

Test cases

case 1
in: [[-1,0,1]]
expected: 2
case 2
in: [[-1,-1]]
expected: 0
case 3
in: [[-1,0,0,2]]
expected: 2
case 4
in: [[]]
expected: 0
Starter codePython
def tree_depth_from_heads(heads):
    # Your code here
    pass

4 test cases · run your code against them in the editor