Question: Given the root pointer to a binary tree, find the height.

    Input: Sample Tree (Pointer to node 1 is given).
    tree_insert_element_1
    Output: 3

    The method we will learn relates to the same method we discussed in the post how to find the size of a binary tree –

    In the above post we applied a recursive approach because the tree follows a recursive property. Every node has the same property. It has a root value, a left pointer and a right pointer. This is seen in the left sub-tree and right sub-tree as well.
    The depth of the binary tree is the height of the tree, or we can say the number of levels in the binary tree. In the above tree we have 3 levels.

    If we see the first node, we have a property:- max (left height, right height) + 1

    So, what we can do is we can recursively calculate the height of left sub-tree and right sub-tree of a node and assign height to the node as max of the height of two children + 1. This is also similar to PreOrder Traversal.

    #include<stdio.h>
    
    struct binaryTreeNode{
    	int data;
    	struct binaryTreeNode * left;
    	struct binaryTreeNode * right;
    };
    
    int heightOfBinaryTreeRecursive(struct binaryTreeNode * root)
    {
    	// Initialize 2 variables for left and right height
    	int leftHeight;
    	int rightHeight;
    	
    	// The terminating case
    	if(root == NULL)
    		return 0;
    	else
    	{
    		// Find the depth of left sub-tree
    		leftHeight = heightOfBinaryTreeRecursive(root -> left);
    		
    		// Find the depth of right sub-tree
    		rightHeight = heightOfBinaryTreeRecursive(root -> right);
    		
    		// Compare the two heights and return the larger of two + 1
    		if(leftHeight > rightHeight)
    		{
    			// Return the leftHeight + 1
    			return (leftHeight + 1);
    		}
    		else
    		{
    			// Return the rightHeight + 1
    			return (rightHeight + 1);
    		}
    	}
    }
    

    Time Complexity:- O(n)
    Space Complexity:- O(n)

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, delete the binary tree Input: Sample Tree (Pointer to node 1 is given). To delete a tree we must traverse all the nodes of the tree and delete them one by one. So which traversal should we use ? Inorder, Pre-order, Post-order or Level Order Traversal ? Before deleting he parent node, we should delete its child node first. We can therefore use post order traversal as it does the work without storing anything. We can delete three with other traversals …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find its size. Input: Sample Tree (Pointer to node 1 is given). Find the size Output: Size = 7 We discussed how to find the size of a binary tree using the recursive method in this post:- Write an algorithm to find the size of a binary tree This technique involved recursion and the understanding can be a bit complicated. If you prefer to go through the iterative way, we can simple do a level order traversal on the tree. At …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find its size. Input: Sample Tree (Pointer to node 1 is given). Find the size Output: Size = 7 The size of the binary tree is basically the number of nodes in a binary tree. Since, the tree follows a recursive approach we can easily solve this problem by recursion. We calculate the size of left and right sub-tree recursively, add 1 (current node) and return to its parent. The recursive implementation can be done as:- #include<stdio.h> struct binaryTreeNode{ int data; …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, write a program to insert an element in it. Input: Sample Tree (Pointer to node 1 is given), Insert 7 Output: After insertion We are given a binary tree with random allocated values and the pointer to the root node is given. Since the given tree is a binary tree, we can insert the element wherever we want. The only property of binary tree is that every node can have a maximum of two children. So, to keep the tree leveled …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find if an element is present in it. Input: Sample Tree (Pointer to node 1 is given), Search – 3 Output: Present We discussed how to find if an element exists in the binary tree in this post. Find if an element is present in a binary tree That involved recursion. If recursion seems difficult to you, we can even solve this problem with the help of level order traversal of binary tree. The property of level order traversal is that …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find if an element is present in it. Input: Sample Tree (Pointer to node 1 is given), Search – 3 Output: Present One simple way of solving this problem is to find the element in the left sub-tree, in the right sub-tree, and in the root data. This approach can be easily implemented with recursion. This approach works because each node of the tree has 3 parts -> the root, the left and the right. What we do is we see …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More