1
这是一个用于查找高度的递归方法,但我在二叉搜索树中有非常多的节点,我想要查找树的高度以及为每个单独的子树分配高度。所以递归方法抛出stackoverflow异常,我怎么做非递归,而不使用堆栈?非递归地查找BST的高度?
private int FindHeight(TreeNode node)
{
if (node == null)
{
return -1;
}
else
{
node.Height = 1 + Math.Max(FindHeight(node.Left), FindHeight(node.Right));
return node.Height;
}
}
我相信我有使用后序遍历,但没有叠加么?