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, 'š
']