What is the difference between tree depth and height?

后端 未结 7 1470
[愿得一人]
[愿得一人] 2020-11-28 00:14

This is a simple question from algorithms theory.
The difference between them is that in one case you count number of nodes and in other number of edges on the shortest

相关标签:
7条回答
  • 2020-11-28 00:48

    The answer by Daniel A.A. Pelsmaeker and Yesh analogy is excellent. I would like to add a bit more from hackerrank tutorial. Hope it helps a bit too.

    • The depth(or level) of a node is its distance(i.e. no of edges) from tree's root node.
    • The height is number of edges between root node and furthest leaf.
    • height(node) = 1 + max(height(node.leftSubtree),height(node.rightSubtree)).
      Keep in mind the following points before reading the example ahead.
    • Any node has a height of 1.
    • Height of empty subtree is -1.
    • Height of single element tree or leaf node is 0.
    0 讨论(0)
提交回复
热议问题