Iterative Method to Find Height of Binary Tree


The binary tree is a data structure. Each node of the binary tree contains either 0, 1, or 2 nodes. So, the binary tree can contain multiple levels.

Here, we need to write the iterative code using the loops to find the height of the binary tree. The total number of levels in the binary tree represents the height of the binary tree. Alternatively, we can say that the maximum depth of the binary tree from the root node is the height of the binary tree.

Problem statement − We have given a binary tree. We need to use the iterative method to find the height of the given binary tree.

Approach 1

As we said above, the height of the binary tree is equal to the total number of levels in the binary tree. We will use the queue data structure to traverse each node of each level and find the maximum depth of the tree.

Algorithm

Step 1 − Define the ‘treeNode’ class, and add the ‘val’ integer variable. Also, define the ‘left’ and ‘right’ pointers in the class.

Step 2 − Define the createNode() function to create a new node for the tree. It creates a new treeNode, initializes the ‘val’ with the parameter value, and left and right pointers with null values. At last, it returns the newly created node.

Step 3 − The findHeight() function is used to find the height of the binary tree.

Step 4 − Define the 'levelqueue' queue to store all nodes of the current level, 'treeHeight', 'n_cnt' variables, and ‘temp' node.

Step 5 − If the head node is Null, return 0.

Step 6 − Push the head node to the 'levelQueue'.

Step 7 − Make iterations using the ‘while’ loop until the ‘levelQueue’ becomes empty.

Step 8 − Increment the ‘treeHeight’ by 1, and initialize the ‘n_cnt’ with the size of the queue, representing the total nodes in the current level.

Step 9 − Traverse all elements of the queue.

Step 9.1 − Pop the first element of the queue.

Step 9.2 − If the left child node exists for the current node, insert it into the queue.

Step 9.3 − If the right child node exists for the current node, insert it into the queue.

Step 9.4 − Remove the first node from the queue.

Step 10 − Return ‘treeHeight’ variable’s value.

Example

#include <iostream>
#include <queue>

using namespace std;
class treeNode {
public:
    int val;
    treeNode *left;
    treeNode *right;
};
treeNode *createNode(int val) {
    //Create a new node
    treeNode *temp = new treeNode();
    temp->val = val;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
int fidHeight(treeNode *head) {
    queue<treeNode *> levelQueue;
    int treeHeight = 0; // To store the tree height of the current binary tree
    int n_cnt = 0;      // To store the total number of current level nodes.
    treeNode *temp;     // Pointer to store the address of a node in the current level.
    // For empty binary tree
    if (head == NULL) {
        return 0;
    }
    // Add root node to queue
    levelQueue.push(head);
    // Traverse level of binary tree
    while (!levelQueue.empty()) {
        // For each level increment, the treeHeight of the three
        treeHeight++;
        n_cnt = levelQueue.size();
        // Add child nodes of all nodes at the current level
        while (n_cnt--) {
            temp = levelQueue.front();
            // Insert the left child node of the current node
            if (temp->left != NULL) {
                levelQueue.push(temp->left);
            }
            // Insert the right child node of the current node
            if (temp->right != NULL) {
                levelQueue.push(temp->right);
            }
            // remove the current node
            levelQueue.pop();
        }
    }
    return treeHeight;
}
int main() {
    treeNode *head = NULL;
    // Adding nodes to binary tree.
    head = createNode(45);
    head->right = createNode(32);
    head->right->left = createNode(48);
    head->left = createNode(90);
    head->left->left = createNode(5);
    head->left->left->left = createNode(50);
    cout << "The given binary tree's treeHeight is " << fidHeight(head) << ".";
    return 0;
}

Output

The given binary tree's treeHeight is 4.

Time complexity − O(N) to traverse through each node.

Space complexity − O(N) to store nodes in the queue.

The iterative approach is always faster than the recursive approach to solve any problem. Here, we used the loop and queue to find the maximum depth or height of the binary tree using iteratively. However, programmers may try to write the code for the recursive approach to find the height of the binary tree.

Updated on: 21-Jul-2023

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements