1
1
/**
2
- * https://leetcode.com/problems/same-tree/
3
- * TIme O(N) | Space O(H)
2
+ * Check if both nodes are null (end of a branch in both trees)
4
3
* @param {TreeNode } p
5
4
* @param {TreeNode } q
6
5
* @return {boolean }
7
6
*/
8
- var isSameTree = function ( p , q ) {
9
- const isBaseCase = ! ( p || q ) ;
10
- if ( isBaseCase ) return true ;
7
+ var isSameTree = function ( p , q ) {
8
+ // Check if both nodes are null (end of a branch in both trees)
9
+ const areBothNodesNull = p == null && q == null ;
10
+ if ( areBothNodesNull ) return true ;
11
11
12
- const isBalanced = ( p && q ) ;
13
- if ( ! isBalanced ) return false ;
12
+ // Check if only one node is null (mismatch in tree structure)
13
+ const isOnlyOneNodeNull = p == null || q == null ;
14
+ if ( isOnlyOneNodeNull ) return false ;
14
15
15
- const isSame = p . val === q . val ;
16
- if ( ! isSame ) return false ;
16
+ // Check if node values are equal (mismatch in node values)
17
+ const doNodesHaveEqualValue = p . val == q . val ;
18
+ if ( ! doNodesHaveEqualValue ) return false ;
17
19
20
+ // Recursively check left and right subtrees
18
21
return dfs ( p , q ) ;
19
22
} ;
20
23
24
+ /**
25
+ * * https://leetcode.com/problems/same-tree/
26
+ * * Time complexity is O(N), where N is the total number of nodes in the tree.
27
+ * This is because in the worst-case scenario, we need to visit every node once.
28
+
29
+ * * Space complexity is O(H), where H is the height of the tree.
30
+ * This is because in the worst-case scenario (a skewed tree), the maximum
31
+ * amount of space is consumed by the recursive stack.
32
+ * @param {* } p
33
+ * @param {* } q
34
+ * @returns
35
+ */
21
36
const dfs = ( p , q ) => {
22
37
const left = isSameTree ( p . left , q . left ) ;
23
38
const right = isSameTree ( p . right , q . right ) ;
24
39
25
40
return left && right ;
26
- }
41
+ } ;
27
42
28
43
/**
29
44
* https://leetcode.com/problems/same-tree/
@@ -32,40 +47,41 @@ const dfs = (p, q) => {
32
47
* @param {TreeNode } q
33
48
* @return {boolean }
34
49
*/
35
- var isSameTree = function ( p , q ) {
50
+ var isSameTree = function ( p , q ) {
36
51
if ( isSameNode ( p , q ) ) return true ;
37
52
38
- return bfs ( [ [ p , q ] ] ) ;
39
- }
53
+ return bfs ( [ [ p , q ] ] ) ;
54
+ } ;
40
55
41
56
const bfs = ( queue ) => {
42
57
while ( queue . length ) {
43
- for ( let i = ( queue . length - 1 ) ; 0 <= i ; i -- ) {
44
- const [ p , q ] = queue . shift ( ) ;
58
+ for ( let i = queue . length - 1 ; 0 <= i ; i -- ) {
59
+ const [ p , q ] = queue . shift ( ) ;
45
60
46
61
if ( ! isSame ( p , q ) ) return false ;
47
62
48
- if ( p . left ) queue . push ( [ p . left , q . left ] ) ;
49
- if ( p . right ) queue . push ( [ p . right , q . right ] ) ;
63
+ if ( p . left ) queue . push ( [ p . left , q . left ] ) ;
64
+ if ( p . right ) queue . push ( [ p . right , q . right ] ) ;
50
65
}
51
66
}
52
67
53
68
return true ;
54
- }
69
+ } ;
55
70
56
71
const isSameNode = ( p , q ) => {
57
72
const isBaseCase = ! ( p || q ) ;
58
73
if ( isBaseCase ) return true ;
59
74
60
- const isBalanced = ( p && q ) ;
75
+ const isBalanced = p && q ;
61
76
if ( ! isBalanced ) return false ;
62
77
63
78
const isSame = p . val === q . val ;
64
79
if ( ! isSame ) return false ;
65
80
66
81
return true ;
67
- }
82
+ } ;
68
83
69
- const isSame = ( p , q ) => isSameNode ( p , q )
70
- && isSameNode ( p . left , q . left )
71
- && isSameNode ( p . right , q . right ) ;
84
+ const isSame = ( p , q ) =>
85
+ isSameNode ( p , q ) &&
86
+ isSameNode ( p . left , q . left ) &&
87
+ isSameNode ( p . right , q . right ) ;
0 commit comments