Height of Binary Tree
The height or depth of a binary tree can be defined as the maximum or the largest number of edges from a leaf node to the root node or root node to the leaf node. The root node will be at level zero that means if the root node doesn't have any of the child nodes connected to it then the height or depth of the particular binary tree is said to be zero.
Let us take an example for a better understanding of the height of the binary tree.
In the above image, we have a binary tree starting from the root node named A. The root node A is having two child nodes B and C as the left child and right child respectively. And similarly left child node B has only one left child node named D and right child node C has two child nodes E and F from which node E has node G as the only left child.
Now let's calculate the height of this binary tree. Count the number of edges starting from the root node to the deepest leaf node for calculating the height of the binary tree. The deepest node that is present in this binary tree is the node G. So, for the calculation of the height or depth of this binary tree we need to calculate the number of edges between the root node and the deepest node G. The first edge is from node A to node C, the second edge is from the node C to node E and the third edge is from the node E to node G. So, for traversing from the root node A to the deepest node G there are three edges, so the height or depth of the binary tree is 3. The path that we followed to move from the root to the deepest leaf node is A > C > E > G and this path covers three edges during the traversal, that is why according to the definition of the height of the binary tree the height of this binary tree is 3.
Ways to Find Height of Binary Tree
Now, let us write code to find the height of a binary tree. There are two ways to find the height of the binary tree. One is the recursive method and the other one is the non-recursive method that will make use of the Queue data structure to calculate the height of the binary tree.
First, let's see the recursive way to find the height of the binary tree.
Output: The output of the above code is:
Printing the nodes of tree level wise: Level order traversal: (level 0) 150 (level 1) 250 270 (level 2) 320 350 The height of the Binary tree is: 2
In a recursive way, we have called the height() function repeatedly to find the height of the binary tree. The root node of the binary tree is passed as a parameter to the height() function. The height() function calculates the height of both the subtrees of the root node and which one among both of the heights is higher is considered as the height of the binary tree.
Now let see the non-recursive way to find the height of the binary tree.
The Height(Depth) of the tree is: 2
In this approach, we have used a non recursive way to find the depth of the binary tree. To find the height of the binary tree, we have written a function named height that will require a parameter of Node type (that means the root of the binary tree whose height needs to be calculated). The root of the binary tree is present at level zero, which means the height or depth of the root is zero.
In the non recursive approach, we use the Queue Data Structure to find the depth of the binary tree. The nodes of the binary tree for which we want to find the depth are added to the Queue data structure with the help of an enqueue operation to which the node of the binary tree is passed as a parameter to this function.
Once all the nodes are added to the queue, the nodes added in the queue are removed by calling the dequeue function that will keep on removing one element from the queue until the null node of the binary tree is encountered. Each time a node of the binary tree from the queue is removed, the depth variable representing the depth of the binary tree is incremented by one. And in the end, the value of the depth variable will represent the final depth of the binary tree.