-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.js
31 lines (24 loc) · 922 Bytes
/
solution.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
27
28
29
30
31
/**
* @typedef {{ value: string; left?: BinaryTree; right?: BinaryTree }} BinaryTree
*/
/**
* @param {BinaryTree | undefined} tree1 - The first binary tree.
* @param {BinaryTree | undefined} tree2 - The second binary tree.
* @returns {[boolean, string]}
*/
export default function isTreesSynchronized(tree1, tree2) {
/** @type {[boolean, string]} */
const result = [tree1?.value === tree2?.value, `${tree1?.value}`]
if (!tree1?.left || !tree2?.right) return result
/** @type {BinaryTree} */
const leftBranch01 = tree1.left
/** @type {BinaryTree | undefined} */
const rightBranch01 = tree1.right
/** @type {BinaryTree | undefined} */
const leftBranch02 = tree2.left
/** @type {BinaryTree} */
const rightBranch02 = tree2.right
result[0] = result[0] && isTreesSynchronized(leftBranch01, rightBranch02)[0]
result[0] = result[0] && isTreesSynchronized(rightBranch01, leftBranch02)[0]
return result
}