File tree Expand file tree Collapse file tree 1 file changed +8
-32
lines changed Expand file tree Collapse file tree 1 file changed +8
-32
lines changed Original file line number Diff line number Diff line change 17
17
*/
18
18
var isSameTree = function ( p , q ) {
19
19
const dfs = ( currentP , currentQ ) => {
20
- if ( currentP . val !== currentQ . val ) {
21
- return false ;
20
+ if ( ! currentP && ! currentQ ) {
21
+ return true ;
22
22
}
23
23
24
- if (
25
- ( currentP . left && ! currentQ . left ) ||
26
- ( ! currentP . left && currentQ . left )
27
- ) {
24
+ if ( ( ! currentP && currentQ ) || ( currentP && ! currentQ ) ) {
28
25
return false ;
29
26
}
30
27
31
- if (
32
- ( currentP . right && ! currentQ . right ) ||
33
- ( ! currentP . right && currentQ . right )
34
- ) {
28
+ if ( currentP . val !== currentQ . val ) {
35
29
return false ;
36
30
}
37
31
38
- if ( currentP . left && currentQ . left ) {
39
- if ( ! dfs ( currentP . left , currentQ . left ) ) {
40
- return false ;
41
- }
42
- }
43
-
44
- if ( currentP . right && currentQ . right ) {
45
- if ( ! dfs ( currentP . right , currentQ . right ) ) {
46
- return false ;
47
- }
48
- }
49
-
50
- return true ;
32
+ return (
33
+ dfs ( currentP . left , currentQ . left ) && dfs ( currentP . right , currentQ . right )
34
+ ) ;
51
35
} ;
52
36
53
- if ( ( p && ! q ) || ( ! p && q ) ) {
54
- return false ;
55
- }
56
-
57
- if ( p && q ) {
58
- return dfs ( p , q ) ;
59
- }
60
-
61
- return true ;
37
+ return dfs ( p , q ) ;
62
38
} ;
You can’t perform that action at this time.
0 commit comments