Skip to content

Commit 8fd5748

Browse files
committed
adding new solution to subtree-of-another-tree javascript problem
1 parent f4a0f0d commit 8fd5748

File tree

1 file changed

+94
-52
lines changed

1 file changed

+94
-52
lines changed

javascript/0572-subtree-of-another-tree.js

Lines changed: 94 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,135 @@
44
* @param {TreeNode} subRoot
55
* @return {boolean}
66
*/
7-
var isSubtree = function(root, subRoot) {
8-
if (!root) return false
7+
var isSubtree = function (root, subRoot) {
8+
if (!root) return false;
99

10-
if (isSame(root, subRoot)) return true
10+
if (isSame(root, subRoot)) return true;
1111

12-
const hasLeftTree = isSubtree(root.left, subRoot)
13-
const hasRightTree = isSubtree(root.right, subRoot)
12+
const hasLeftTree = isSubtree(root.left, subRoot);
13+
const hasRightTree = isSubtree(root.right, subRoot);
1414

15-
return hasLeftTree || hasRightTree
15+
return hasLeftTree || hasRightTree;
1616
};
1717

1818
const isSame = (root, subRoot) => {
19-
const hasReachedEnd = !(root && subRoot)
20-
if (hasReachedEnd) return root === subRoot
19+
const hasReachedEnd = !(root && subRoot);
20+
if (hasReachedEnd) return root === subRoot;
2121

22-
const isMismatch = root.val !== subRoot.val
23-
if (isMismatch) return false
22+
const isMismatch = root.val !== subRoot.val;
23+
if (isMismatch) return false;
2424

25-
const isLeftSame = isSame(root.left, subRoot.left)
26-
const isRightSame = isSame(root.right, subRoot.right)
25+
const isLeftSame = isSame(root.left, subRoot.left);
26+
const isRightSame = isSame(root.right, subRoot.right);
2727

28-
return isLeftSame && isRightSame
29-
}
28+
return isLeftSame && isRightSame;
29+
};
3030

31-
const hash = (val) => require('crypto')
32-
.createHash('md5')
33-
.update(val)
34-
.digest('hex')
31+
const hash = (val) =>
32+
require('crypto').createHash('md5').update(val).digest('hex');
3533

3634
const merkle = (root) => {
37-
if (!root) return '#'
35+
if (!root) return '#';
3836

39-
const { left, val, right } = root
37+
const { left, val, right } = root;
4038

41-
const leftMerkle = merkle(left)
42-
const rightMerkle = merkle(right)
39+
const leftMerkle = merkle(left);
40+
const rightMerkle = merkle(right);
4341

44-
const merkleVal = [ leftMerkle, val, rightMerkle ].join('')
45-
const merkleHash = hash(merkleVal)
42+
const merkleVal = [leftMerkle, val, rightMerkle].join('');
43+
const merkleHash = hash(merkleVal);
4644

47-
root.merkle = merkleHash
45+
root.merkle = merkleHash;
4846

49-
return root.merkle
50-
}
47+
return root.merkle;
48+
};
5149

5250
const search = (root, subRoot) => {
53-
if (!root) return false
51+
if (!root) return false;
5452

55-
const hasSamePath = root.merkle === subRoot.merkle
56-
if (hasSamePath) return true
53+
const hasSamePath = root.merkle === subRoot.merkle;
54+
if (hasSamePath) return true;
5755

58-
const left = search(root.left, subRoot)
59-
const right = search(root.right, subRoot)
56+
const left = search(root.left, subRoot);
57+
const right = search(root.right, subRoot);
6058

61-
return left || right
62-
}
59+
return left || right;
60+
};
6361

64-
var isSubtree = function(root, subRoot) {
65-
[ root, subRoot ].forEach(merkle)
62+
var isSubtree = function (root, subRoot) {
63+
[root, subRoot].forEach(merkle);
6664

67-
return search(root, subRoot)
68-
}
65+
return search(root, subRoot);
66+
};
6967

7068
const hashify = (root, hash, postOrderKey) => {
71-
if (!root) return '#'
69+
if (!root) return '#';
7270

73-
const left = hashify(root.left, hash, postOrderKey)
74-
const right = hashify(root.right, hash, postOrderKey)
71+
const left = hashify(root.left, hash, postOrderKey);
72+
const right = hashify(root.right, hash, postOrderKey);
7573

76-
const key = [left, root.val, right].join('')
74+
const key = [left, root.val, right].join('');
7775

7876
if (!hash.has(key)) {
79-
hash.set(key, postOrderKey[0])
80-
postOrderKey[0]++
77+
hash.set(key, postOrderKey[0]);
78+
postOrderKey[0]++;
8179
}
8280

83-
return hash.get(key)
84-
}
81+
return hash.get(key);
82+
};
8583

86-
var isSubtree = function(root, subRoot, hash = new Map (), postOrderKey = [0]) {
87-
hashify(root, hash, postOrderKey)
84+
var isSubtree = function (root, subRoot, hash = new Map(), postOrderKey = [0]) {
85+
hashify(root, hash, postOrderKey);
8886

8987
const hashKey = [
9088
hashify(subRoot.left, hash, postOrderKey),
9189
subRoot.val,
92-
hashify(subRoot.right, hash, postOrderKey)
93-
].join('')
90+
hashify(subRoot.right, hash, postOrderKey),
91+
].join('');
92+
93+
return hash.has(hashKey);
94+
};
9495

95-
return hash.has(hashKey)
96-
}
96+
/**
97+
* Definition for a binary tree node.
98+
* function TreeNode(val, left, right) {
99+
* this.val = (val===undefined ? 0 : val)
100+
* this.left = (left===undefined ? null : left)
101+
* this.right = (right===undefined ? null : right)
102+
* }
103+
*/
104+
/**
105+
* https://leetcode.com/problems/subtree-of-another-tree/
106+
* @param {TreeNode} root
107+
* @param {TreeNode} subRoot
108+
* @return {boolean}
109+
*/
110+
var isSubtree = function (root, subRoot) {
111+
if (!subRoot) {
112+
return true;
113+
} else if (!root) {
114+
return false;
115+
} else if (isSameTree(root, subRoot)) {
116+
return true;
117+
}
118+
119+
const leftResult = isSubtree(root.left, subRoot);
120+
const rightResult = isSubtree(root.right, subRoot);
121+
122+
return leftResult || rightResult;
123+
};
124+
125+
function isSameTree(root, subRoot) {
126+
if (!root && !subRoot) {
127+
return true;
128+
} else if (!root || !subRoot) {
129+
return false;
130+
} else if (root.val !== subRoot.val) {
131+
return false;
132+
}
133+
134+
const leftRes = isSameTree(root.left, subRoot.left);
135+
const rightRes = isSameTree(root.right, subRoot.right);
136+
137+
return leftRes && rightRes;
138+
}

0 commit comments

Comments
 (0)