Skip to content

Instantly share code, notes, and snippets.

@huytd
Created March 5, 2020 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huytd/81880fe17525de9ebd724078d2569f9b to your computer and use it in GitHub Desktop.
Save huytd/81880fe17525de9ebd724078d2569f9b to your computer and use it in GitHub Desktop.
const root = {
val: 1,
left: {
val: 2,
left: null,
right: null
},
right: {
val: 3,
left: null,
right: null
}
};
const schemalize = obj => {
const schema = [];
for (let key in obj) {
schema.push({
name: key,
type: typeof obj[key]
});
}
return schema;
};
const compareSchema = (a, b) => {
if (a.length != b.length) return false;
for (let i = 0; i < a.length; i++) {
if (a[i].name !== b[i].name || a[i].type !== b[i].type) {
return false;
}
}
return true;
};
const isSameSchema = schemas => !schemas.some(s => !compareSchema(schemas[0], s));
const rootSchema = schemalize(root);
const leftSchema = schemalize(root.left);
const rightSchema = schemalize(root.right);
const result = isSameSchema([rootSchema, leftSchema, rightSchema]);
debug(result);
debug(rootSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment