How to calculate the depth of a binary search tree

前端 未结 10 1849
说谎
说谎 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:47

    For any given tree, the number of nodes is 1 for the root plus the number of nodes in the left subtree plus the number of nodes in the right subtree :)

    Details, like making sure there actually is a left or right subtree, are "left to the reader".

提交回复
热议问题