/// <summary>
/// 计算获取一棵树某一节点的层数
/// </summary>
/// <returns></returns>
public int GetNodeLevelCount(TreeNode tvNode)
{
if (tvNode == null)
return 0;
if (tvNode.Nodes.Count <= 0)
return 1;
int childrenMaxCount = 0;
foreach (TreeNode node in tvNode.Nodes)
{
if (childrenMaxCount <= GetTreeLevelCount(node)) //递归获取最高的子节点
childrenMaxCount = GetTreeLevelCount(node);
}
return childrenMaxCount + 1; //返回
}
/// <summary>
/// 计算一棵树的高度
/// </summary>
/// <param name="tv"></param>
/// <returns></returns>
public int GetTreeLevelCount(TreeView tv)
{
if (tv == null)
return 0;
if (tv.Nodes.Count <= 0)
return 0;
int maxCount = 0;
foreach (TreeNode node in tv.Nodes)
{
if (maxCount <= GetNodeLevelCount(node))
maxCount = GetNodeLevelCount(node);
}
return maxCount;
}