【发布时间】:2013-02-13 03:26:09
【问题描述】:
【问题讨论】:
-
二叉树的高度是多少?最大高度?最小高度?
-
这是检查它是否高度平衡。因为我是编码新手,所以我不知道如何构建它。我需要知道左子树的最大高度和右子树的最大高度
-
正在寻找该函数以使用上面的源查找高度。
标签: c# binary-tree binary-search-tree subtree
【问题讨论】:
标签: c# binary-tree binary-search-tree subtree
好吧,如果它是 BST 的正确实现,那么它们应该是平衡的。
但是为了测试它,这里有一个简单的递归实现。
public int TreeDepth( TreeNode<T> tree, int depth = 0 )
{
int leftDepth = tree.Left != null
? TreeDepth( tree.Left, depth + 1 )
: depth;
int rightDepth = tree.Right != null
? TreeDepth( tree.Right, depth + 1 )
: depth;
return leftDepth >= rightDepth
? leftDepth
: rightDepth;
}
【讨论】: