【问题标题】:Finding the Height of a Binary Search Tree in Java using Vertex使用 Vertex 在 Java 中查找二叉搜索树的高度
【发布时间】:2013-12-17 00:02:08
【问题描述】:

我在编写此方法 public int getHeight 时遇到问题,我必须使用递归找到二叉搜索树的高度。原因是我的主类是由私有可比较 mKey 和私有顶点 mLeft、mRight 和 mParent 组成的,而不是节点。希望得到一些帮助。

【问题讨论】:

  • 请贴出你代码的相关部分

标签: java recursion binary-search-tree


【解决方案1】:
public int getHeight(Tree t, int depth)
{
    if(t == null){
        return depth;
    }
    else{
        return Math.max(getHeight(t.mLeft, depth + 1), getHeight(t.mRight, depth + 1))
    }
}

这将返回树中最深分支的长度。

编辑: 拨打getHeight(tree,0)

【讨论】:

    猜你喜欢
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多