在二进制搜索树中查找高度

mik*_*ike 55 binary-tree

我想知道是否有人可以帮我修改这个方法来找到二叉搜索树的高度.到目前为止,我的代码看起来像这样.但是,我得到的答案比实际高度大1.然而当我从我的返回语句中删除+1时,它比实际高度小1.我仍然试图用我的头围绕递归这些BST.任何帮助将非常感激.

public int findHeight(){
    if(this.isEmpty()){
        return 0;
    }
    else{
        TreeNode<T> node = root;
        return findHeight(node);
    }
}
private int findHeight(TreeNode<T> aNode){
    int heightLeft = 0;
    int heightRight = 0;
    if(aNode.left!=null)
        heightLeft = findHeight(aNode.left);
    if(aNode.right!=null)
        heightRight = findHeight(aNode.right);
    if(heightLeft > heightRight){
        return heightLeft+1;
    }
    else{
        return heightRight+1;
    }
}
Run Code Online (Sandbox Code Playgroud)

Cor*_*rey 108

问题出在你的基础案例中.

"树的高度是树中从根到最深节点的路径长度.只有一个节点(根)的(根)树的高度为零." - 维基百科

如果没有节点,则要返回-1而不是0.这是因为最后添加1.

因此,如果没有节点,则返回-1,取消+1.

int findHeight(TreeNode<T> aNode) {
    if (aNode == null) {
        return -1;
    }

    int lefth = findHeight(aNode.left);
    int righth = findHeight(aNode.right);

    if (lefth > righth) {
        return lefth + 1;
    } else {
        return righth + 1;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,这可以正常工作,而不必在我的公共方法中减去1.我仍然对这种方法如何与递归一起工作感到困惑.我在if语句之后左右声明了int,但我不明白它们是如何通过这种方法递增的 (2认同)
  • 这种方法的工作原理是在基本情况下减去 1,它们像给定的所有其他方法一样递增,当您深入到树中时,您将高度加 1。 (2认同)
  • 对于那些对这种递归如何工作感到困惑的人,我制作了一个视频,首先在高层次上解释了这个过程,然后我们通过一个例子来运行代码:[查找二叉树的高度](https:// www. youtube.com/watch?v=AWIJwNf0ZQE).希望你们中的许多人会觉得它很有用,在这种情况下我们应该把它添加到这个答案中. (2认同)

Ste*_*hen 32

二叉搜索树的高度等于number of layers - 1.

请参阅http://en.wikipedia.org/wiki/Binary_tree上的图表

你的递归是好的,所以只需在根级别减去一次.

另请注意,您可以通过处理空节点来清理函数:

int findHeight(node) {
  if (node == null) return 0;
  return 1 + max(findHeight(node.left), findHeight(node.right));
}
Run Code Online (Sandbox Code Playgroud)

  • 对于单节点树,高度为 0。所以对于空树,高度为 -1。这就是为什么当节点为 NULL 时 `return -1` 有效而不是 `return 0` (3认同)
  • @Matthew:你是对的,但我建议他的公共函数从结果中减去一个.相反,您可以通过在基本情况下返回-1来"修复"递归函数. (2认同)

小智 20

int getHeight(Node node) {
 if (node == null) return -1;

 return 1 + Math.max(getHeight(node.left), getHeight(node.right));
}
Run Code Online (Sandbox Code Playgroud)


Jer*_*fin 9

IMO,您的代码将受益于简化一点.当子指针为null时,不要尝试结束递归,而只在当前指针为空时结束它.这使得代码很多容易编写.在伪代码中,它看起来像这样:

if (node = null)
    return 0;
else
    left = height(node->left);
    right = height(node->right);
    return 1 + max(left, right);
Run Code Online (Sandbox Code Playgroud)

  • @jemfinch,他在一个空对象上调用它,是不是基本情况是什么? (4认同)

小智 5

class Solution{
    public static int getHeight(Node root) {
        int height = -1;

        if (root == null) {
            return height;
        } else {
            height = 1 + Math.max(getHeight(root.left), getHeight(root.right));
        }

        return height;
    }
Run Code Online (Sandbox Code Playgroud)


lin*_*dyk 5

对于像我这样喜欢单行解决方案的人:

public int getHeight(Node root) {
    return Math.max(root.left != null ? getHeight(root.left) : -1, 
                    root.right != null ? getHeight(root.right) : -1) 
                    + 1;
}
Run Code Online (Sandbox Code Playgroud)