MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / isSameTree

Function isSameTree

javascript/0100-same-tree.js:7–22  ·  view source on GitHub ↗
(p, q)

Source from the content-addressed store, hash-verified

5 * @return {boolean}
6 */
7var 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/

Callers 1

dfsFunction · 0.70

Calls 3

isSameNodeFunction · 0.85
dfsFunction · 0.70
bfsFunction · 0.70

Tested by

no test coverage detected