-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path05-Check-Two-Binary-Trees-Are-Same-LC-100.js
81 lines (63 loc) · 1.52 KB
/
05-Check-Two-Binary-Trees-Are-Same-LC-100.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
100. Same Tree
Easy
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
Input: 1 1
/ \ / \
2 3 2 3
[1,2,3], [1,2,3]
Output: true
Input: 1 1
/ \
2 2
[1,2], [1,null,2]
Output: false
Input: 1 1
/ \ / \
2 1 1 2
[1,2,1], [1,1,2]
Output: false
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
var queue1 = [];
var queue2 = [];
queue1.push(p);
queue2.push(q);
while (queue1.length && queue2.length) {
var node1 = queue1.shift();
var node2 = queue2.shift();
var val1;
var val2;
if (node1 === null) {
val1 = null;
} else {
val1 = node1.val;
queue1.push(node1.left);
queue1.push(node1.right);
}
if (node2 === null) {
val2 = null;
} else {
val2 = node2.val;
queue2.push(node2.left);
queue2.push(node2.right);
}
if (val1 !== val2) {
return false;
}
}
return queue1.length === queue2.length;
};