File tree Expand file tree Collapse file tree 5 files changed +131
-0
lines changed
maximum-depth-of-binary-tree Expand file tree Collapse file tree 5 files changed +131
-0
lines changed Original file line number Diff line number Diff line change 1+ // Time complexity : O(n)
2+ // Space complexity : O(n)
3+
4+ var climbStairs = function ( n ) {
5+ // create a memoization object to store results.
6+ const memo = { } ;
7+
8+ // recursive function to calculate the number of ways to climb n steps.
9+ function climb ( n ) {
10+ // if n is 1 or 2, there's only one way to climb or two ways.
11+ if ( n <= 2 ) return n ;
12+
13+ // if the result for n is already computed, return it from memo.
14+ if ( memo [ n ] ) return memo [ n ] ;
15+
16+ // otherwise, compute the result recursively.
17+ memo [ n ] = climb ( n - 1 ) + climb ( n - 2 ) ;
18+ return memo [ n ] ;
19+ }
20+
21+ return climb ( n ) ;
22+ } ;
Original file line number Diff line number Diff line change 1+ // Time complexity : O(n)
2+ // Space complexity : O(n)
3+
4+ var maxDepth = function ( root ) {
5+ // if the root is null, the depth is 0.
6+ if ( root === null ) {
7+ return 0 ;
8+ }
9+
10+ // initialize the stack with the root node and its depth, which is 1.
11+ const stack = [ { node : root , depth : 1 } ] ;
12+ let maxDepth = 0 ;
13+
14+ // process the stack till it is empty.
15+ while ( stack . length > 0 ) {
16+ // pop the top element from the stack.
17+ const { node, depth } = stack . pop ( ) ;
18+
19+ if ( node !== null ) {
20+ // update the maximum depth.
21+ maxDepth = Math . max ( maxDepth , depth ) ;
22+
23+ // push the left and right children with their respective depths.
24+ if ( node . left !== null ) {
25+ stack . push ( { node : node . left , depth : depth + 1 } ) ;
26+ }
27+ if ( node . right !== null ) {
28+ stack . push ( { node : node . right , depth : depth + 1 } ) ;
29+ }
30+ }
31+ }
32+
33+ return maxDepth ;
34+ } ;
Original file line number Diff line number Diff line change 1+ // Time Complexity: O(n log n)
2+ // Space Complexity: O(1)
3+
4+ class Solution {
5+ canAttendMeetings ( intervals ) {
6+ intervals . sort ( ( a , b ) => a . start - b . start ) ;
7+
8+ // check for overlaps.
9+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
10+ // check if there is an overlap between the current interval and the previous one.
11+ if ( intervals [ i ] . start < intervals [ i - 1 ] . end ) {
12+ return false ;
13+ }
14+ }
15+
16+ return true ;
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ // Time complexity : O(n)
2+ // Space complexity : O(n)
3+
4+ var isSameTree = function ( p , q ) {
5+ // initialize two queues for BFS.
6+ let queue1 = [ p ] ;
7+ let queue2 = [ q ] ;
8+
9+ while ( queue1 . length > 0 && queue2 . length > 0 ) {
10+ let node1 = queue1 . shift ( ) ;
11+ let node2 = queue2 . shift ( ) ;
12+
13+ // if both nodes are null, continue to the next pair of nodes.
14+ if ( node1 === null && node2 === null ) {
15+ continue ;
16+ }
17+
18+ // if one of the nodes is null or their values are different, return false.
19+ if ( node1 === null || node2 === null || node1 . val !== node2 . val ) {
20+ return false ;
21+ }
22+
23+ // enqueue the left and right children of both nodes.
24+ queue1 . push ( node1 . left ) ;
25+ queue1 . push ( node1 . right ) ;
26+ queue2 . push ( node2 . left ) ;
27+ queue2 . push ( node2 . right ) ;
28+ }
29+
30+ // if both queues are empty, the trees are the same
31+ return queue1 . length === 0 && queue2 . length === 0 ;
32+ } ;
Original file line number Diff line number Diff line change 1+ // Time complexity : O(m * n)
2+ // Space complexity : O(h)
3+
4+ // fuunction to check if two trees are identical.
5+ var isIdentical = function ( root1 , root2 ) {
6+ // if they are both empty, return true.
7+ if ( ! root1 && ! root2 ) return true ;
8+ // if either one of them is null and the other is not, return false.
9+ if ( ! root1 || ! root2 ) return false ;
10+
11+ // if the right subtrees are identical, return true only if all conditions are met.
12+ return root1 . val === root2 . val &&
13+ isIdentical ( root1 . left , root2 . left ) &&
14+ isIdentical ( root1 . right , root2 . right ) ;
15+ }
16+
17+ var isSubtree = function ( root , subRoot ) {
18+ // if the root is empty, return false
19+ if ( ! root ) return false ;
20+ // if the subtree rooted at subRoot is identical, return true.
21+ if ( isIdentical ( root , subRoot ) ) return true ;
22+
23+ // if find the subtree in either the left or right subtree, return true. otherwise, return false.
24+ return isSubtree ( root . left , subRoot ) || isSubtree ( root . right , subRoot ) ;
25+ } ;
You can’t perform that action at this time.
0 commit comments