-
Notifications
You must be signed in to change notification settings - Fork 0
/
872.leaf-similar-trees.js
76 lines (67 loc) · 1.26 KB
/
872.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* @lc app=leetcode id=872 lang=javascript
*
* [872] Leaf-Similar Trees
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
function dfs(tree, ret) {
if (tree) {
if (!tree.left && !tree.right) {
ret.push(tree.val);
}
dfs(tree.left, ret);
dfs(tree.right, ret);
}
}
/**
* @param {TreeNode} root1
* @param {TreeNode} root2
* @return {boolean}
*/
var leafSimilar = function(root1, root2) {
let ret1 = [];
let ret2 = [];
dfs(root1, ret1);
dfs(root2, ret2);
for (let i = 0; i < ret1.length; i++) {
if (ret1[i] !== ret2[i]) {
return false;
}
}
return true;
};
function dfs(stack) {
while (true) {
let node = stack.pop();
if (node.right) {
stack.push(node.right);
}
if (node.left) {
stack.push(node.left);
}
if (!node.left && !node.right) {
return node.val;
}
}
}
/**
* @param {TreeNode} root1
* @param {TreeNode} root2
* @return {boolean}
*/
var leafSimilar = function(root1, root2) {
let s1 = [root1];
let s2 = [root2];
while (s1.length !== 0 && s2.length !== 0) {
if (dfs(s1) !== dfs(s2)) {
return false;
}
}
return s1.length === 0 && s2.length === 0;
};