Skip to content

Commit 6b478f3

Browse files
committed
solve: same tree
1 parent d769f97 commit 6b478f3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

same-tree/evan.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {TreeNode} p
3+
* @param {TreeNode} q
4+
* @return {boolean}
5+
*/
6+
var isSameTree = function (p, q) {
7+
if (!p && !q) {
8+
return true;
9+
}
10+
11+
if (!p || !q || p.val !== q.val) {
12+
return false;
13+
}
14+
15+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
16+
};
17+
18+
/**
19+
* Time Complexity: O(n)
20+
* Reason: We visit each node exactly once.
21+
*
22+
* Space Complexity: O(n)
23+
* Reason:
24+
* The space used by the call stack is proportional
25+
* to the height of the tree.
26+
*/

0 commit comments

Comments
 (0)