The height of a node in a binary tree is the largest variety of edges in a path from a sheet node come a target node.

You are watching: How to find height of binary search tree

If the target node doesn’t have any type of other nodes associated to it, the elevation of that node would be . The elevation of a binary tree is the height of the source node in the whole binary tree. In various other words, the elevation of a binary tree is equal to the largest variety of the edge from the root to the most far-off leaf node.

A comparable concept in a binary tree is the depth that the tree. The depth of a node in a binary tree is the total variety of edges indigenous the source node come the target node. Similarly, the depth that a binary tree is the total number of edges indigenous the root node to the most far-off leaf node.

One crucial observation below is the when we calculate the depth of a whole binary tree, it is equivalent to the elevation of the binary tree.

Let’s take a binary tree: Firstly, we’re calculating the elevation of the node . So, follow to the definition, the height of the node is the largest number of edges in a route from the sheet node come the node . We deserve to see for the node , there are two paths: and . The largest number of edges among these two courses would be . For this reason the height of node is .

Now, let’s calculation the elevation of the binary tree. From the root, we deserve to have three different paths bring about the leaf nodes: , , and . Amongst these 3 paths, the course contains the most number of edges that is . Therefore the height of the tree is .

Next, we want to discover the depth that the node . Currently we deserve to see the from the root, over there is only one path to the node , and it has one edge. Hence the depth of the node is . The depth of a binary tree is same to the height of the tree. Therefore, the depth of the binary tree is .

## 4. Algorithm We start the algorithm by taking the root node together an input. Next, we calculate the elevation of the left and right boy nodes that the root. In instance the source doesn’t have any type of child node, us return the elevation of the tree as .

We recursively contact all the nodes from the left and also right subtree that the root node to calculate the elevation of a binary tree. Finally, as soon as we calculate the elevation of the left and also right subtree, us take the best height among both and include one to it. The number return by the algorithm would certainly be the elevation of the binary tree.

## 5. Time intricacy Analysis

In the ideal case, we can have just one node in the binary tree. In together a case, we just execute the an initial condition of the algorithm when the root is null and return the height of the tree as . Here, the time complexity would be .

In general, us calculate the height of every node in the tree. We call all the nodes recursively, calculate the elevation of the left and also right subtree native the root node, and finally return the elevation of the totality binary tree. Hence, us visit all the nodes in the tree.

See more: How Long Does Nucynta Stay In Your System ? How Long Does Tapentadol Stay In Your System

Let’s consider the variety of nodes in a binary tree is . Therefore, the time intricacy would be .