I am doing a refresher an algorithms and data structures.
I am confused on the concept of depth vs height of a tree. In many cases, especially on sites focusing on interview quizes, it seems to me that these terms are used interchangeably.
It seems to me that the basic literature defines them as applicable to a node and not to a tree.
So the depth of the root (which is a node) is 0
. The height of root (or any subnode) is the max height of its children.
But when you apply these terms on a tree i.e. find the max depth of a tree, it seems that these terms now are "meaningless" and can be used interchangeably i.e. to find the max depth just calculate max height.
For example in this post Check if tree is balanced the answers focus on the height of the tree while the definition of balance could be on the depth of the tree
Is my understanding correct or am I messing up on these fundamentals?
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.
Depth. In a tree, many edges from the root node to the particular node are called the depth of the tree. In the tree, the total number of edges from the root node to the leaf node in the longest path is known as "Depth of Tree". In the tree data structures, the depth of the root node is 0.
Level – The level of a node is defined by 1 + the number of connections between the node and the root. Simply, level is depth plus 1. The important thing to remember is when talking about level, it starts from 1 and the level of the root is 1.
When talking about a tree they mean the same thing: the length of the longest path from the root to a leaf node.
The depth is usually used to describe a property of a tree node, while the height is used to describe the property of the entire tree, as in the following examples:
The height of a tree is defined as the depth of its deepest node.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With