(p, q)
| 5 | * @return {boolean} |
| 6 | */ |
| 7 | var isSameTree = function (p, q) { |
| 8 | // Check if both nodes are null (end of a branch in both trees) |
| 9 | const areBothNodesNull = p == null && q == null; |
| 10 | if (areBothNodesNull) return true; |
| 11 | |
| 12 | // Check if only one node is null (mismatch in tree structure) |
| 13 | const isOnlyOneNodeNull = p == null || q == null; |
| 14 | if (isOnlyOneNodeNull) return false; |
| 15 | |
| 16 | // Check if node values are equal (mismatch in node values) |
| 17 | const doNodesHaveEqualValue = p.val == q.val; |
| 18 | if (!doNodesHaveEqualValue) return false; |
| 19 | |
| 20 | // Recursively check left and right subtrees |
| 21 | return dfs(p, q); |
| 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * * https://leetcode.com/problems/same-tree/ |
no test coverage detected