在一次采访中提出了这个问题:给出了具有黑白节点的树。在给定的树中找到最长的白色节点路径。下面的方法是否正确或有人帮助提供更好的方法谢谢!
int Longest(node root, int max)
{
if(root==null || root.color == black)
return 0;
if(root.color == white)
{
int curmax =1+ firstlongest(root.child) + secondlongest(root.child);
if(curmax>max)
max = curmax;
return curmax;
}
if(root.color == black)
{
for(all children)
{
int curmax =1+ firstlongest(root.child) + secondlongest(root.child);
}
if(curmax>max)
max =curmax;
return 0;
}
}
int firstlongest(node* child){//will calculate first longest of children and similarly
secondlongest gives second.Finally max will have length of longest path.