using System.Collections.Generic;
public static void Main()
var l = new List<int> {-1, 8, 6, 0, 7, 3, 8, 9, -1, 6, 1};
Console.WriteLine(GetTreeHeight(l));
static int GetTreeHeight(List<int> nodes)
var dt = new DepthTracker(nodes);
public class DepthTracker{
public DepthTracker(List<int> nodes){
Depth = new int[Nodes.Count];
for (var i = 0; i < Nodes.Count; i++)
if (Nodes[i] > -1 && Depth[i] == 0)
Depth[i] = CheckDepth(i);
public int CheckDepth(int index)
if(index == -1) return 0;
var nextIdx = Nodes[index];
var thisDepth = CheckDepth(nextIdx) + 1;
Depth[index] = thisDepth;