-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
56 lines (49 loc) · 1.2 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root1
* @param {TreeNode} root2
* @return {boolean}
*/
var leafSimilar = function(root1, root2) {
let leafs1 = [],
leafs2 = [],
stack1 = [],
stack2 = []
if (root1) stack1.push(root1)
if (root2) stack2.push(root2)
while (stack1.length > 0) {
let node = stack1.pop()
if (node.right) {
stack1.push(node.right)
}
if (node.left) {
stack1.push(node.left)
}
if (!node.left && !node.right) {
leafs1.push(node.val)
}
}
while (stack2.length > 0) {
let node = stack2.pop()
if (node.right) {
stack2.push(node.right)
}
if (node.left) {
stack2.push(node.left)
}
if (!node.left && !node.right) {
leafs2.push(node.val)
}
}
if (leafs1.length !== leafs2.length) return false
for (let i = 0; i < leafs1.length; i++) {
if (leafs1[i] !== leafs2[i]) return false
}
return true
};