We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4b8d6d5 commit 62c6327Copy full SHA for 62c6327
same-tree/nhistory.js
@@ -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