How to calculate the depth of a binary search tree

前端 未结 10 1848
说谎
说谎 2020-12-05 15:11

I would like to calculate the summation of the depths of each node of a Binary Search Tree.

The individual depths of the elements are not already stored.

10条回答
  •  粉色の甜心
    2020-12-05 15:35

    private static int getNumberOfNodes(Node node) {
        if (node == null) {
            return 0;
        }
    
        return 1 + getNumberOfNodes(node.left) + getNumberOfNodes(node.right);
    }
    

提交回复
热议问题