check at each level if the node exist or not and traverse completely.
private boolean sameStructure(TreeNode ref, TreeNode otherRef)
{
if (otherRef == null && ref != null)
return false;
if (otherRef != null && ref == null)
return false;
if (otherRef == null && ref == null)
return true;
return sameStructureHelp(ref.left, otherRef.left) &&
sameStructureHelp(ref.right, otherRef.right);
}