At the North Pole, the elves have two magical binary trees that generate energy ๐ฒ๐ฒ to keep the Christmas star โญ๏ธ shining. However, for them to work properly, the trees must be in perfect sync like mirrors ๐ช.
Two binary trees are mirrors if:
And the tree is represented with three properties value, left, and right. The latter two display the remaining branches (if any):
const tree = {
value: 'โญ๏ธ',
left: {
value: '๐
'
// left: {...}
// right: { ... }
},
right: {
value: '๐'
// left: { ... }
// right: { ... }
}
}
Santa needs your help to verify if the trees are synchronized so that the star can keep shining. You must return an array where the first position indicates if the trees are synchronized and the second position returns the value of the root of the first tree.
const tree1 = {
value: '๐',
left: { value: 'โญ' },
right: { value: '๐
' }
}
const tree2 = {
value: '๐',
left: { value: '๐
' }
right: { value: 'โญ' },
}
isTreesSynchronized(tree1, tree2) // [true, '๐']
/*
tree1 tree2
๐ ๐
/ \ / \
โญ ๐
๐
โญ
*/
const tree3 = {
value: '๐',
left: { value: '๐
' },
right: { value: '๐' }
}
isTreesSynchronized(tree1, tree3) // [false, '๐']
const tree4 = {
value: '๐',
left: { value: 'โญ' },
right: { value: '๐
' }
}
isTreesSynchronized(tree1, tree4) // [false, '๐']
isTreesSynchronized(
{ value: '๐
' },
{ value: '๐งโ๐' }
) // [false, '๐
']

