forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnhistory.js
More file actions
24 lines (23 loc) · 664 Bytes
/
nhistory.js
File metadata and controls
24 lines (23 loc) · 664 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
// If p and q is null, return true
if (!p && !q) return true;
// Compare root and length between p and q
if (!p || !q || p.val !== q.val) return false;
// Execute recursive function to search each tree
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};
// TC: O(n)
// SC: O(n)