File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * TC: O(N)
3
+ * SC: O(N)
4
+ * N: count of node in tree
5
+ */
6
+
7
+ /**
8
+ * Definition for a binary tree node.
9
+ * function TreeNode(val, left, right) {
10
+ * this.val = (val===undefined ? 0 : val)
11
+ * this.left = (left===undefined ? null : left)
12
+ * this.right = (right===undefined ? null : right)
13
+ * }
14
+ */
15
+ /**
16
+ * @param {TreeNode } p
17
+ * @param {TreeNode } q
18
+ * @return {boolean }
19
+ */
20
+ var isSameTree = function ( p , q ) {
21
+ const queueP = [ p ] ;
22
+ const queueQ = [ q ] ;
23
+
24
+ while ( queueP . length > 0 && queueQ . length > 0 ) {
25
+ const currentP = queueP . shift ( ) ;
26
+ const currentQ = queueQ . shift ( ) ;
27
+
28
+ if ( currentP === null && currentQ === null ) {
29
+ continue ;
30
+ }
31
+
32
+ if ( currentP === null || currentQ === null ) {
33
+ return false ;
34
+ }
35
+
36
+ if ( currentP . val !== currentQ . val ) {
37
+ return false ;
38
+ }
39
+
40
+ queueP . push ( currentP . left ) ;
41
+ queueP . push ( currentP . right ) ;
42
+
43
+ queueQ . push ( currentQ . left ) ;
44
+ queueQ . push ( currentQ . right ) ;
45
+ }
46
+
47
+ return queueP . length === 0 && queueQ . length === 0 ;
48
+ } ;
You can’t perform that action at this time.
0 commit comments