Question: Given the root pointer to a binary tree, find the LCA (Least Common Ancestor) of two nodes in the tree.
    Input: Sample Tree (Pointer to node 1 is given). Find LCA of node 5 and 4
    Output: Answer = 3

    A simple recursive approach can be used to find the LCA (Least common ancestor).

    #include<stdio.h>
    #include<malloc.h>
    
    struct binaryTreeNode{
    	int data;
    	struct binaryTreeNode * left;
    	struct binaryTreeNode * right;
    };
    
    struct binaryTreeNode * LCA(struct binaryTreeNode * root, struct binaryTreeNode * a, struct binaryTreeNode * b)
    {
    	struct binaryTreeNode * left, *right;
    	
    	// The terminating condition
    	if(root == NULL)
    		return root;
    	
    	// If one of the elements is among the input nodes, then we have got our LCA
    	if(root == a || root == b)
    		return root;
    	
    	// Search in the left sub tree
    	left = LCA(root -> left, a, b);
    	
    	// Search in the right sub tree
    	right = LCA(root -> right, a, b);
    	
    	if(left && right)
    		return root;
    	else
    		return (left ? left : right)
    }
    
    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given 2 binary trees, determine if they are mirror of each other Input: 2 tree pointers are given. (Root pointer) Output: TRUE We can again apply the recursive sub-structure property of the tree here. The algorithm can be implemented as:- #include<stdio.h> #include<malloc.h> struct binaryTreeNode{ int data; struct binaryTreeNode * left; struct binaryTreeNode * right; }; int areTreeMirrors(struct binaryTreeNode * root1, struct binaryTreeNode * root2) { // Terminating condition if(root1 == NULL && root2 == NULL) return 1; //If one of them is NULL, then they are definitely not mirrors …

    1 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given the root pointer to a binary tree, find the sum of all its elements without using recursion. Input: Sample Tree (Pointer to node 1 is given). Output: Sum = 15 We discussed how to find the sum of all elements of a binary tree using recursion in this post. Given a binary tree, find the sum of all elements. If recursion seems tough to you , you can always go by the iterative approach and use Level Order Traversal of binary tree. While performing a level order traversal, …

    0 FacebookTwitterLinkedinWhatsappEmail
  • 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). 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) { …

    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

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