如何计算二叉搜索树的深度

时间:2009-12-09 19:32:23

标签: java recursion binary-search-tree

我想计算二进制搜索树的每个节点深度的总和。

尚未存储元素的各个深度。

10 个答案:

答案 0 :(得分:17)

这样的事情:

int countChildren(Node node)
{
    if ( node == null )
        return 0;
    return 1 + countChildren(node.getLeft()) + countChildren(node.getRight());
}

获得每个孩子的深度总和:

int sumDepthOfAllChildren(Node node, int depth)
{
    if ( node == null )
        return 0;  // starting to see a pattern?
    return depth + sumDepthOfAllChildren(node.getLeft(), depth + 1) + 
                   sumDepthOfAllChildren(node.getRight(), depth + 1);
}

现在有一个希望提供信息的解释,以防这是家庭作业。计算节点数非常简单。首先,如果节点不是节点(node == null),则返回0.如果节点是节点,则首先计算其自身(1),加上其中的节点数。左子树加上其右子树中的节点数。另一种思考方式是通过BFS访问每个节点,并为您访问的每个节点添加一个计数。

深度的总和是类似的,除了不为每个节点仅添加一个,该节点添加其自身的深度。它知道自己的深度,因为它的父母告诉它。每个节点都知道它的子节点的深度是它自己的深度加一,所以当你得到一个节点的左右子节点的深度时,你告诉它们它们的深度是当前节点的深度加1.

同样,如果节点不是节点,则它没有深度。因此,如果您想要所有根节点的子节点的深度总和,您将传递根节点和根节点的深度,如下所示:sumDepthOfAllChildren(root, 0)

递归是非常有用的,它只是一种非常不同的思考事物的方式,并且需要练习才能适应它

答案 1 :(得分:12)

int maxDepth(Node node) {
    if (node == null) {
        return (-1); // an empty tree  has height −1
    } else {
        // compute the depth of each subtree
        int leftDepth = maxDepth(node.left);
        int rightDepth = maxDepth(node.right);
        // use the larger one
        if (leftDepth > rightDepth )
            return (leftDepth + 1);
        else
            return (rightDepth + 1);
    }
}

答案 2 :(得分:3)

对于任何给定的树,根的节点数为1加上左子树中的节点数加上右子树中的节点数:)

详细信息,例如确保实际 左或右子树,“留给读者”。

答案 3 :(得分:3)

这个解决方案更加简单。

public int getHeight(Node root)
{
    if(root!=null)
        return 1+ Math.max(getHeight(root.leftchild),getHeight(root.rightchild));
    else
        return 0;
}

答案 4 :(得分:2)

private static int getNumberOfNodes(Node node) {
    if (node == null) {
        return 0;
    }

    return 1 + getNumberOfNodes(node.left) + getNumberOfNodes(node.right);
}

答案 5 :(得分:1)

public class Node {
   private Node left; 
   private Node right;
   public int size() { return 1+ (left==null?0:left.size())+ (right==null?0:right.size());}
}

答案 6 :(得分:1)

int depth(treenode *p)
{
   if(p==NULL)return(0);
   if(p->left){h1=depth(p->left);}
   if(p=>right){h2=depth(p->right);}
   return(max(h1,h2)+1);
}

答案 7 :(得分:0)

public int numberOfNodes()
{
   // This node.
   int result = 1;

   // Plus all the nodes from the left node.
   Node left = getLeft();
   if (left != null)
       result += left.numberOfNodes();

   // Plus all the nodes from the right node.
   Node right = getRight();
   if (right != null)
       result += right.numberOfNodes();

   return result;
}

答案 8 :(得分:0)

public int countNodes(Node root)
{  
   // Setup
   // assign to temps to avoid double call accessors. 
   Node left = root.getLeft();
   Node right = root.getRight();
   int count = 1; // count THIS node.

   // count subtrees
   if (left != null) count += countNodes(left);
   if (right != null) count += countNodes(right);

   return count;
}

答案 9 :(得分:-2)

public int getDepthHelper( TreeNode< T > node ) { 
    int treeHeightLeft; 
    int treeHeightRight; 
    //get height of left subtree 
    if( node.leftNode == null ) 
        treeHeightLeft = 1; 
    else { 
        treeHeightLeft = getDepthHelper( node.leftNode) + 1; 
    } 

    //get height of right subtree 
    if( node.rightNode == null ) 
        treeHeightRight = 1; 
    else { 
        treeHeightRight = getDepthHelper( node.rightNode) + 1; 
    } 
    return Math.max(treeHeightLeft, treeHeightRight); 
}
相关问题