@@ -13,28 +13,34 @@ class TreeNode {
13
13
*/
14
14
const isSameTree = ( p , q ) => {
15
15
if ( p ?. val !== q ?. val ) return false ;
16
- let pQueue = [ p ] , qQueue = [ q ] ;
17
-
18
- const extractLeft = ( node , list ) => {
19
- if ( null === node ) return ;
20
- list . push ( node . left ) ;
21
- return node . left ?. val ;
22
- }
23
-
24
- const extractRight = ( node , list ) => {
25
- if ( null == node ) return ;
26
- list . push ( node . right ) ;
27
- return node . right ?. val ;
28
- }
29
-
30
- while ( 0 < pQueue . length && 0 < qQueue . length ) {
31
- let currP = pQueue . shift ( ) , currQ = qQueue . shift ( ) ;
32
- if ( currP ?. val !== currQ ?. val ) return false ;
33
- extractLeft ( currP , pQueue )
34
- extractLeft ( currQ , qQueue )
35
- extractRight ( currP , pQueue )
36
- extractRight ( currQ , qQueue )
37
- }
38
-
39
- return true ;
16
+ if ( null === p && null === q ) return true ;
17
+ return isSameTree ( p . left , q . left ) && isSameTree ( p . right , q . right ) ;
40
18
} ;
19
+
20
+ // const isSameTree = (p, q) => {
21
+ // if (p?.val !== q?.val) return false;
22
+ // let pQueue = [p], qQueue = [q];
23
+ //
24
+ // const extractLeft = (node, list) => {
25
+ // if (null === node) return;
26
+ // list.push(node.left);
27
+ // return node.left?.val;
28
+ // }
29
+ //
30
+ // const extractRight = (node, list) => {
31
+ // if (null == node) return;
32
+ // list.push(node.right);
33
+ // return node.right?.val;
34
+ // }
35
+ //
36
+ // while (0 < pQueue.length && 0 < qQueue.length) {
37
+ // let currP = pQueue.shift(), currQ = qQueue.shift();
38
+ // if (currP?.val !== currQ?.val) return false;
39
+ // extractLeft(currP, pQueue)
40
+ // extractLeft(currQ, qQueue)
41
+ // extractRight(currP, pQueue)
42
+ // extractRight(currQ, qQueue)
43
+ // }
44
+ //
45
+ // return true;
46
+ // };
0 commit comments