Skip to content

Commit 35459a3

Browse files
committed
refactor: 100. Same Tree
1 parent bf5a3c6 commit 35459a3

File tree

1 file changed

+8
-32
lines changed

1 file changed

+8
-32
lines changed

same-tree/gwbaik9717.js

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,46 +17,22 @@
1717
*/
1818
var isSameTree = function (p, q) {
1919
const dfs = (currentP, currentQ) => {
20-
if (currentP.val !== currentQ.val) {
21-
return false;
20+
if (!currentP && !currentQ) {
21+
return true;
2222
}
2323

24-
if (
25-
(currentP.left && !currentQ.left) ||
26-
(!currentP.left && currentQ.left)
27-
) {
24+
if ((!currentP && currentQ) || (currentP && !currentQ)) {
2825
return false;
2926
}
3027

31-
if (
32-
(currentP.right && !currentQ.right) ||
33-
(!currentP.right && currentQ.right)
34-
) {
28+
if (currentP.val !== currentQ.val) {
3529
return false;
3630
}
3731

38-
if (currentP.left && currentQ.left) {
39-
if (!dfs(currentP.left, currentQ.left)) {
40-
return false;
41-
}
42-
}
43-
44-
if (currentP.right && currentQ.right) {
45-
if (!dfs(currentP.right, currentQ.right)) {
46-
return false;
47-
}
48-
}
49-
50-
return true;
32+
return (
33+
dfs(currentP.left, currentQ.left) && dfs(currentP.right, currentQ.right)
34+
);
5135
};
5236

53-
if ((p && !q) || (!p && q)) {
54-
return false;
55-
}
56-
57-
if (p && q) {
58-
return dfs(p, q);
59-
}
60-
61-
return true;
37+
return dfs(p, q);
6238
};

0 commit comments

Comments
 (0)