forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwogha95.js
More file actions
48 lines (40 loc) ยท 997 Bytes
/
wogha95.js
File metadata and controls
48 lines (40 loc) ยท 997 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* TC: O(N)
* SC: O(N)
* N: count of node in tree
*/
/**
* 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) {
const queueP = [p];
const queueQ = [q];
while (queueP.length > 0 && queueQ.length > 0) {
const currentP = queueP.shift();
const currentQ = queueQ.shift();
if (currentP === null && currentQ === null) {
continue;
}
if (currentP === null || currentQ === null) {
return false;
}
if (currentP.val !== currentQ.val) {
return false;
}
queueP.push(currentP.left);
queueP.push(currentP.right);
queueQ.push(currentQ.left);
queueQ.push(currentQ.right);
}
return queueP.length === 0 && queueQ.length === 0;
};