C# 如何求C中左右子树的高度#

C# 如何求C中左右子树的高度#,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 ) : d

如何找到左边和右边的子树高度,我在下面的链接中使用了这段代码

任何帮助都将不胜感激


好吧,如果这是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;
}
public int-TreeDepth(树节点树,int-depth=0)
{
int leftDepth=tree.Left!=null
树深(树左,深度+1)
:深度;
int rightDepth=tree.Right!=null
树深(树右,深度+1)
:深度;
返回leftDepth>=rightDepth
?左深度
:右深度;
}

二叉树的高度是多少?最大高度?最小高度?这是为了检查它是否高度平衡。由于我对编码还不熟悉,我不知道如何构建这个框架。我需要知道左子树的最大高度和右子树的最大高度。我正在使用上面的源代码查找该函数以查找高度。它是正确实现的,我已经检查了测试代码。我想知道如何调用TreeDepth以及要传递哪些参数。谢谢你的帮助@kaerbery你只需给它传递一棵你想要测量的树,比如:var depth=TreeDepth(root);