-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask24.js
26 lines (22 loc) · 856 Bytes
/
task24.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function checkIsSameTree(treeA, treeB) {
function serializeTree(tree) {
let retorno = '' + tree.value
if ( tree.left !== null ) retorno += 'L' + serializeTree(tree.left)
if ( tree.right !== null ) retorno += 'R' + serializeTree(tree.right)
return retorno
}
return ( serializeTree(treeA) === serializeTree(treeB) ? true : false )
}
const tree = {
value: 1,
left: { value: 2, left: null, right: null },
right: { value: 3, left: null, right: null }
}
console.log(checkIsSameTree(tree, tree), ' // true')
const tree2 = {
value: 1,
left: { value: 3, left: { value: 2, left: null, right: null }, right: null },
right: { value: 5, left: null, right: { value: 4, left: null, right: null } }
}
console.log(checkIsSameTree(tree, tree2), ' // false')
console.log(checkIsSameTree(tree2, tree2), ' // true')