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 1717 */
1818var isSameTree = function ( p , q ) {
1919 const dfs = ( currentP , currentQ ) => {
20- if ( currentP . val !== currentQ . val ) {
21- return false ;
20+ if ( ! currentP && ! currentQ ) {
21+ return true ;
2222 }
2323
24- if (
25- ( currentP . left && ! currentQ . left ) ||
26- ( ! currentP . left && currentQ . left )
27- ) {
24+ if ( ( ! currentP && currentQ ) || ( currentP && ! currentQ ) ) {
2825 return false ;
2926 }
3027
31- if (
32- ( currentP . right && ! currentQ . right ) ||
33- ( ! currentP . right && currentQ . right )
34- ) {
28+ if ( currentP . val !== currentQ . val ) {
3529 return false ;
3630 }
3731
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+ ) ;
5135 } ;
5236
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 ) ;
6238} ;
You can’t perform that action at this time.
0 commit comments