Skip to content

Commit 170948b

Browse files
committed
🆒 [100] Better recursion
1 parent a562a4c commit 170948b

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

100/solution.js

+30-24
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,34 @@ class TreeNode {
1313
*/
1414
const isSameTree = (p, q) => {
1515
if (p?.val !== q?.val) return false;
16-
let pQueue = [p], qQueue = [q];
17-
18-
const extractLeft = (node, list) => {
19-
if (null === node) return;
20-
list.push(node.left);
21-
return node.left?.val;
22-
}
23-
24-
const extractRight = (node, list) => {
25-
if (null == node) return;
26-
list.push(node.right);
27-
return node.right?.val;
28-
}
29-
30-
while (0 < pQueue.length && 0 < qQueue.length) {
31-
let currP = pQueue.shift(), currQ = qQueue.shift();
32-
if (currP?.val !== currQ?.val) return false;
33-
extractLeft(currP, pQueue)
34-
extractLeft(currQ, qQueue)
35-
extractRight(currP, pQueue)
36-
extractRight(currQ, qQueue)
37-
}
38-
39-
return true;
16+
if (null === p && null === q) return true;
17+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
4018
};
19+
20+
// const isSameTree = (p, q) => {
21+
// if (p?.val !== q?.val) return false;
22+
// let pQueue = [p], qQueue = [q];
23+
//
24+
// const extractLeft = (node, list) => {
25+
// if (null === node) return;
26+
// list.push(node.left);
27+
// return node.left?.val;
28+
// }
29+
//
30+
// const extractRight = (node, list) => {
31+
// if (null == node) return;
32+
// list.push(node.right);
33+
// return node.right?.val;
34+
// }
35+
//
36+
// while (0 < pQueue.length && 0 < qQueue.length) {
37+
// let currP = pQueue.shift(), currQ = qQueue.shift();
38+
// if (currP?.val !== currQ?.val) return false;
39+
// extractLeft(currP, pQueue)
40+
// extractLeft(currQ, qQueue)
41+
// extractRight(currP, pQueue)
42+
// extractRight(currQ, qQueue)
43+
// }
44+
//
45+
// return true;
46+
// };

0 commit comments

Comments
 (0)