Help with approach

https://hack.codingblocks.com/app/contests/2022/381/problem

approach:
just check whether the roots of the trees have the same structure (e.g - if one trees left child is NULL then the other trees left child should also be NULL)
if they have return true and call on the left and right subtree else return false
base case: if root is NULL then return true

will this work?

this is not correct
because depth of the left and right sub-trees of every node can differ by 1 or less

correct Approach

binary tree T is balanced if:

  1. Left subtree of T is balanced
  2. Right subtree of T is balanced
  3. The difference between heights of left subtree and right subtree is not more than 1.

To check if a tree is height-balanced, get the height of left and right subtrees. Return true if difference between heights is not more than 1 and left and right subtrees are balanced, otherwise return false…

baseCase
if(root==NULL) return true;
// An empty tree is height-balanced

I’m sorry I sent the wrong link
https://hack.codingblocks.com/app/contests/2022/74/problem
this is the correct one

for this question your approach will work

Reference Code

if your doubt resolved plz mark it as resolved
else feel free to ask


please check my code it is failing 2 test cases

if(root1-> left == NULL){
		if(root2 -> left == NULL){
			return true;
		}
		else{
			return false;
		}
	}
	if(root1 -> right == NULL){
		if(root2->right == NULL){
			return true;
		}
		else{
			return false;
		}
	}

these condition are not complete
root2->left may be null but root1->left may not

correct conditions
if(root1==NULL and root2==NULL) return true; //both ar null
if(root1==NULL or root2==NULL) return false;
//now both can't be null so if any one is null that means other is not null so return false;

bool IsStructural(node* root1, node* root2){

if(root1 == NULL || root2 == NULL){

    return false;

}

if(root1-> left == NULL){

    if(root2 -> left == NULL){

        return true;

    }

    else{

        return false;

    }

}

else if(root1 -> right == NULL){

    if(root2->right == NULL){

        return true;

    }

    else{

        return false;

    }

}

if (root1 == NULL and root2 == NULL){

    return true;

}

return true;

}
is this correct?

put this condition at top
like this

bool IsStructural(node* root1, node* root2){

if (root1 == NULL and root2 == NULL){

    return true;

}

if(root1 == NULL || root2 == NULL){

    return false;

}

if(root1-> left == NULL){

    if(root2 -> left == NULL){

        return true;

    }

    else{

        return false;

    }

}

else if(root1 -> right == NULL){

    if(root2->right == NULL){

        return true;

    }

    else{

        return false;

    }

}


return (call left) and (call right)

}


I made the changes but still 2 test cases are failing

in the main function

**if(IsStructural){**

        cout<<"true";

    }

    else{

        cout<<"false";

    }

what is this??
pass the arguments to the function

correct one
if(IsStructural){root1,root2)

oh god!! thanks a lot why were even 2 test cases passing before?

Beacause your code always give true
and output of 2 testcases is also true;

2 testcases even passed if you just write cout<<" true"; :smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.