File tree Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change 24
24
* @param {TreeNode } q
25
25
* @returns {boolean }
26
26
*/
27
+ /**
28
+ * Memo: compare root and children recursively.
29
+ * Complex: O(nlgn)
30
+ * Runtime: ms
31
+ * Tests: 54 test cases passed
32
+ * Rank: S
33
+ */
34
+ var isSameTree = function ( p , q ) {
35
+ if ( ( ! p && q ) || ( p && ! q ) ) return false ; // one is null
36
+ if ( ! p && ! q ) return true ; // cautious both null nodes generates true
37
+ if ( p . val !== q . val ) return false ; // val not equal
38
+ return isSameTree ( p . left , q . left ) && isSameTree ( p . right , q . right ) ; // check children
39
+ } ;
40
+
27
41
var isSameTree = function ( p , q ) {
28
42
if ( ( p && ! q ) || ( ! p && q ) ) {
29
43
return false ;
@@ -34,7 +48,9 @@ var isSameTree = function(p, q) {
34
48
35
49
if ( p . val !== q . val ) {
36
50
return false ;
37
- } else {
51
+ } else {
38
52
return isSameTree ( p . left , q . left ) && isSameTree ( p . right , q . right ) ;
39
53
}
40
54
} ;
55
+
56
+ console . log ( isSameTree ( null , null ) ) ;
You can’t perform that action at this time.
0 commit comments