forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind Duplicate Subtrees.js
63 lines (62 loc) · 1.63 KB
/
Find Duplicate Subtrees.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
/**
* 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} root
* @return {TreeNode[]}
*/
var findDuplicateSubtrees = function(root) {
let stack = [];
let result = [];
let hashTable = {};
stack.push(root);
while (stack.length) {
let node = stack.pop();
let treeToArr = dfsArr(node);
if (node.right) {
stack.push(node.right);
}
if (node.left) {
stack.push(node.left);
}
// If sub tree has not been seen, set to one.
// If sub tree has been seen, increment number.
hashTable[treeToArr] = hashTable[treeToArr] ?
{ num: hashTable[treeToArr].num + 1, ref: node} :
{ num: 1, ref: node};
}
// Builds result array with duplicate trees seen
for (let i in hashTable) {
if (hashTable[i].num > 1) {
result.push(hashTable[i].ref);
}
}
return result;
};
// Post order of subtree stored in array
function dfsArr (node) {
let stack = [];
let result = [];
stack.push(node);
while (stack.length) {
let node = stack.pop();
node ? result.push(node.val) : result.push(null);
if (!node) continue;
if (node.right) {
stack.push(node.right);
} else {
stack.push(null);
}
if (node.left) {
stack.push(node.left);
} else {
stack.push(null);
}
}
return result;
}