-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathLeaf-Similar Trees.js
44 lines (38 loc) · 1.04 KB
/
Leaf-Similar Trees.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
/*
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root1
* @param {TreeNode} root2
* @return {boolean}
*/
var leafSimilar = function(root1, root2) {
let array1 = [], array2 = [];
let leaf1 = getLeaf(root1, array1),
leaf2 = getLeaf(root2, array2);
if (leaf1.length !== leaf2.length){ // if different lengths, return false right away
return false;
}
for(let i = 0; i < leaf1.length; i++){
if (leaf1[i] !== leaf2[i]){ // compare pair by pair
return false;
}
}
return true;
};
var getLeaf = function(root, array){ // DFS
if (!root){
return;
}
if (!(root.left || root.right)){
array.push(root.val); // push leaf value to the array
}
getLeaf(root.left, array);
getLeaf(root.right, array);
return array;
}