Ask Question
15 December, 16:31

Write a recursive function that calculates if two binary trees are similar?

+1
Answers (1)
  1. 15 December, 19:51
    0
    bool identicaltrees (Node * root1, Node * root2) / /function of type boolean true if idenctical false if not.

    {

    if (root1==NULL&&root2==NULL) / /both trees are null means identical.

    return true;

    if (roo1 && root2)

    {

    if (root1->data==root2->data) / /condition for recursive call ...

    {

    return (identicaltrees (root1->left, root2->right) &&identicaltrees (root1->right&&root2->right);

    }

    }

    else

    return false;

    }

    Explanation:

    In this function it of type boolean returns true if both the trees are identical return false if not. First we are checking root node of both the trees if both are null then they are identical returning true.

    If both root nodes are not null then checking their data. If data is same then recursively traversing on both trees and checking both trees.

    else returning false.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a recursive function that calculates if two binary trees are similar? ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers