File tree Expand file tree Collapse file tree 5 files changed +152
-0
lines changed
binary-tree-level-order-traversal
lowest-common-ancestor-of-a-binary-search-tree
remove-nth-node-from-end-of-list
validate-binary-search-tree Expand file tree Collapse file tree 5 files changed +152
-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 levelOrder = function ( root ) {
5+ // if the root is null, return an empty array.
6+ if ( root === null ) return [ ] ;
7+
8+ const result = [ ] ;
9+ const queue = [ root ] ;
10+
11+ // while there are nodes in the queue,
12+ while ( queue . length > 0 ) {
13+ const levelSize = queue . length ;
14+ const currentLevel = [ ] ;
15+
16+ // loop nodes in the current level.
17+ for ( let i = 0 ; i < levelSize ; i ++ ) {
18+ // dequeue the front node.
19+ const currentNode = queue . shift ( ) ;
20+ // add value to the current level array.
21+ currentLevel . push ( currentNode . val ) ;
22+ // enqueue left child if exists.
23+ if ( currentNode . left ) queue . push ( currentNode . left ) ;
24+ // enqueue right child if exists.
25+ if ( currentNode . right ) queue . push ( currentNode . right ) ;
26+ }
27+
28+ // add the current level array to the result.
29+ result . push ( currentLevel ) ;
30+ }
31+
32+ return result ;
33+ } ;
Original file line number Diff line number Diff line change 1+ // Time Complexity: O(n)
2+ // Space Complexity: O(1)
3+
4+ var lowestCommonAncestor = function ( root , p , q ) {
5+ // start from the root.
6+ let current = root ;
7+
8+ // traverse the tree.
9+ while ( current !== null ) {
10+ // if both p and q are greater than current node, LCA lies in the right.
11+ if ( p . val > current . val && q . val > current . val ) {
12+ current = current . right ;
13+ }
14+ // if both p and q are smaller than current node, LCA lies in the left.
15+ else if ( p . val < current . val && q . val < current . val ) {
16+ current = current . left ;
17+ }
18+ // if one of p or q is on one side and the other is on the other side, It's LCA.
19+ else {
20+ return current ;
21+ }
22+ }
23+
24+ // if the tree is empty.
25+ return null ;
26+ } ;
Original file line number Diff line number Diff line change 1+ // Time Complexity: O(n)
2+ // Space Complexity: O(1)
3+
4+ var removeNthFromEnd = function ( head , n ) {
5+ // calculate the length of the linked list.
6+ let length = 0 ;
7+ let current = head ;
8+ while ( current !== null ) {
9+ length ++ ;
10+ current = current . next ;
11+ }
12+
13+ // determine the position to remove from the start.
14+ let removeIndex = length - n ;
15+
16+ // if the node to be removed is the head, return the next node.
17+ if ( removeIndex === 0 ) {
18+ return head . next ;
19+ }
20+
21+ // traverse to the node just before the node to be removed.
22+ current = head ;
23+ for ( let i = 0 ; i < removeIndex - 1 ; i ++ ) {
24+ current = current . next ;
25+ }
26+
27+ // remove the nth node from the end.
28+ current . next = current . next . next ;
29+
30+ // return the modified list.
31+ return head ;
32+ } ;
Original file line number Diff line number Diff line change 1+ // Time Complexity: O(n)
2+ // Space Complexity: O(n)
3+
4+ var reorderList = function ( head ) {
5+ // push all nodes onto a stack.
6+ let stack = [ ] ;
7+ let current = head ;
8+ while ( current ) {
9+ stack . push ( current ) ;
10+ current = current . next ;
11+ }
12+
13+ // reorder the list.
14+ let n = stack . length ;
15+ current = head ;
16+
17+ for ( let i = 0 ; i < Math . floor ( n / 2 ) ; i ++ ) {
18+ let next = current . next ;
19+ let last = stack . pop ( ) ;
20+
21+ current . next = last ;
22+ last . next = next ;
23+ current = next ;
24+ }
25+ // ensure the last node points to null.
26+ if ( current ) current . next = null ;
27+ } ;
Original file line number Diff line number Diff line change 1+ // Time Complexity: O(n)
2+ // Space Complexity: O(n)
3+
4+ var isValidBST = function ( root ) {
5+ if ( root === null ) {
6+ return true ;
7+ }
8+
9+ // initialize a queue for BFS.
10+ let queue = [ ] ;
11+ queue . push ( { node : root , min : - Infinity , max : Infinity } ) ;
12+
13+ while ( queue . length > 0 ) {
14+ // dequeue the front one.
15+ let { node, min, max } = queue . shift ( ) ;
16+
17+ // check the BST for the current node.
18+ if ( node . val <= min || node . val >= max ) {
19+ return false ;
20+ }
21+
22+ // enqueue the left child with updated min and max.
23+ if ( node . left !== null ) {
24+ queue . push ( { node : node . left , min : min , max : node . val } ) ;
25+ }
26+
27+ // enqueue the right child with updated min and max.
28+ if ( node . right !== null ) {
29+ queue . push ( { node : node . right , min : node . val , max : max } ) ;
30+ }
31+ }
32+
33+ return true ;
34+ } ;
You can’t perform that action at this time.
0 commit comments