Skip to content

Commit 62c6327

Browse files
committed
Added same tree solution
1 parent 4b8d6d5 commit 62c6327

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

same-tree/nhistory.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {boolean}
13+
*/
14+
var isSameTree = function (p, q) {
15+
// If p and q is null, return true
16+
if (!p && !q) return true;
17+
// Compare root and length between p and q
18+
if (!p || !q || p.val !== q.val) return false;
19+
// Execute recursive function to search each tree
20+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
21+
};
22+
23+
// TC: O(n)
24+
// SC: O(n)

0 commit comments

Comments
 (0)