All test cases are not giving right answers

private boolean structurallyIdentical(Node t, Node o) {

                if(t==null && o==null)
                    return true;
                if((t==null && o!=null) || t!=null && o==null)
                    return false;

                if(t.data==o.data)
                {
                    boolean l=structurallyIdentical(t.left, o.left);
                    boolean r=structurallyIdentical(t.right, o.right);
                    if(l==true && r==true)
                    {
                        return true;
                    }
                    else
                        return false;
                }
                else
                    return false;


	}

this is the function code. it is working fine on dry run. what to change?

@Kapsime_S,
Your code looks alright. Can you please share your complete code with me as well?

import java.util.*;
public class ISsame {

public Scanner scn=new Scanner(System.in);
private class BinaryTree {
	private class Node {
		int data;
		Node left;
		Node right;
	}

	private Node root;
	private int size;

	public BinaryTree() {
		this.root = this.takeInput(null, false);
	}

	public Node takeInput(Node parent, boolean ilc) {

		int cdata = scn.nextInt();
		Node child = new Node();
		child.data = cdata;
		this.size++;

		// left
		boolean hlc = scn.nextBoolean();

		if (hlc) {
			child.left = this.takeInput(child, true);
		}

		// right
		boolean hrc = scn.nextBoolean();

		if (hrc) {
			child.right = this.takeInput(child, false);
		}

		// return
		return child;
	}

	public boolean structurallyIdentical(BinaryTree other) {
		return this.structurallyIdentical(this.root, other.root);
	}

	private boolean structurallyIdentical(Node t, Node o) {
                
                if(t==null && o==null)
                    return true;
                if((t==null && o!=null) || t!=null && o==null)
                    return false;

                if(t.data==o.data)
                {
                    boolean l=structurallyIdentical(t.left, o.left);
                    boolean r=structurallyIdentical(t.right, o.right);
                    if(l==true && r==true)
                    {
                        return true;
                    }
                    else
                        return false;
                }
                else
                    return false;


	}

}
    public static void main(String[] args) {
	ISsame m = new ISsame();
	BinaryTree bt1 = m.new BinaryTree();
	BinaryTree bt2 = m.new BinaryTree();
	System.out.println(bt1.structurallyIdentical(bt2));
}

}

@Kapsime_S,
I have corrected your code: https://ide.geeksforgeeks.org/u3UDrGoJ49
Its working now perfectly.
Error:
if(t.data==o.data)
don’t use this condition.

Ok, But why was this not working ?

@Kapsime_S,
Because we have to compare only the structure and not the data.

But it depends on the Node data

@Kapsime_S,
Structurally identical trees are trees that have same structure. They may or may not have the same data though.

As mentioned in the question, we only have to compare the structures of the tree. The data in the nodes is not our concern.

1 Like

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.