Question: Given the root pointer to a binary tree, find the sum of all its elements.

    Input: Sample Tree (Pointer to node 1 is given).
    tree_insert_element_1
    Output: Sum = 15

    This is a very simple problem. Since the tree follows a recursive sub-structure, we can recursively call left tree sum, right tree sum and then add their values to the current node data.

    #include<stdio.h>
    #include<malloc.h>
    
    struct binaryTreeNode{
    	int data;
    	struct binaryTreeNode * left;
    	struct binaryTreeNode * right;
    };
    
    int sumOfBinaryTree(struct binaryTreeNode * root)
    {
    	// Terminating condition
    	if(root == NULL)
    	{
    		return 0;
    	}
    	else
    	{
    		//Recursively call the addition of root + left + right
    		return (root -> data + sumOfBinaryTree(root -> left) + sumOfBinaryTree(root -> right));
    	}
    }
    
    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Trees

    Given a binary tree, construct its mirror tree.

    by nikoo28
    1 minutes read

    Question: Given a binary tree, find its mirror tree. Input: A tree pointer is given. (Root pointer) Output: Pointer of mirrored tree. We can easily get the mirrored tree by a simple recursive algorithm. We traverse the tree and swap the left and right pointers. #include<stdio.h> #include<malloc.h> struct binaryTreeNode{ int data; struct binaryTreeNode * left; struct binaryTreeNode * right; }; struct binaryTreeNode * mirrorOfBinaryTree(struct binaryTreeNode * root) { struct binaryTreeNode * temp; if(root) { mirrorOfBinaryTree(root -> left); mirrorOfBinaryTree(root -> right); // Swap the pointers in this node temp = root …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given two binary trees, find if they are structurally identical. Input: 2 trees are given. (Root pointers) Output: TRUE When we say structurally identical trees, we mean that both the trees have exactly the same number of nodes, arranged in the exactly same way. It is not necessary that the value of each node should also be the same. Since, we need to traverse each node of the tree and each node has the same property of having a data value, a left child and a right child, we …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find the height. Input: Sample Tree (Pointer to node 1 is given). Output: 3 We discussed the recursive method to find the height of the binary tree in this post- Find the height of the binary tree The non-recursive method will definitely require the level order traversal technique. The level order traversal will traverse the above tree in following manner- 1 2, 3 4, 5 If we are able to keep a track for end of each level, we can easily …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find the number of full nodes. Input: Sample Tree (Pointer to node 1 is given). Find the number of full nodes. Output: Number of full nodes = 3 According to the definition, the set of all nodes with both left and right child are called as full nodes. In the above example, we have the nodes – 1. 2, 3 as full nodes. We can perform a level order traversal and count the number of nodes, who have their right child …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find the number of leaves. Input: Sample Tree (Pointer to node 1 is given). Find the number of leaves. Output: Number of leaves = 4 According to the definition, the set of all nodes whose both left and right child are null, are known as leaf nodes. In the above example, we have the nodes – 7, 9, 5, 4 as the leaf nodes. We can perform a level order traversal and count the number of nodes, who have their right …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find the deepest node. Input: Sample Tree (Pointer to node 1 is given). Output: 5 According to the definition, the deepest node in a binary tree is the last element in the binary tree while performing a level order traversal. In the above case, therefore we have “5” as the deepest node in the tree. We can simply follow a level order traversal approach to solve this problem. Just return the last element to get the deepest node. Here is the …

    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