Why 2 test case are fail in this code

import java.util.*;
public class Main {

static Scanner scn = new Scanner(System.in);

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

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 tnode, Node onode) {
		// write your code 
			if(tnode==null && onode==null){
			return true;
		}
		if(tnode==null || onode==null){
			return false;
		}
		return tnode.data==onode.data
		&& structurallyIdentical(tnode.left,onode.left)
		&& structurallyIdentical(tnode.right,onode.right);
	}

}

}

the main problem was in the takeInput() method. I corrected it, please have a look into this method:

import java.util.*;
public class Main {
static Scanner scn = new Scanner(System.in);
public static void main(String[] args) {
Main m = new Main();
BinaryTree bt1 = m.new BinaryTree();
BinaryTree bt2 = m.new BinaryTree();
System.out.println(bt1.structurallyIdentical(bt2));
}

private class BinaryTree {
private Node root;
private int size;
private class Node {
int data;
Node left;
Node right;
}
public BinaryTree() {
this.root = new Node();
this.root.data = scn.nextInt();
this.root.left = takeInput();
this.root.right = takeInput();
this.size++;
}

public Node takeInput() {

    boolean isData = scn.nextBoolean();
    if(!isData){
        return null;
    }

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

	// left
	child.left = takeInput();

	// right
	child.right = takeInput();
	// return
	return child;
}

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

private boolean structurallyIdentical(Node tnode, Node onode) {
	// write your code 
		if(tnode==null && onode==null){
		return true;
	}
	if(tnode==null || onode==null){
		return false;
	}
	return tnode.data==onode.data
	&& structurallyIdentical(tnode.left,onode.left)
	&& structurallyIdentical(tnode.right,onode.right);
}

}

}

Thanks

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.