Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

Leetcode Problem 100-199

100. Same Tree 🌟

Intuitive recursive solution

Recursive to iterative using queue


101. Symmetric Tree 🌟

O(N) Time solution

O(N) Time, using 2 queue, iterative solution

O(N) Time, using 1 queue, iterative solution


102. Binary Tree Level Order Traversal 🌟🌟

O(N) Time and O(N) Space


103. Binary Tree Zigzag Level Order Traversal 🌟🌟

This question is in continuation with A general approach to level order traversal questions series.

Prerequisites:

  1. Binary tree level order traversal
  2. Binary tree level order traversal - II

Recursive solution

Iterative solution

Without reverse, insert at front if odd (Fastest)


104. Maximum Depth of Binary Tree 🌟

O(N) Time and O(H) Space,(DFS) More proffered than iterative

O(N) Time and O(N) Space (BFS), using level order traversal


105. Construct Binary Tree from Preorder and Inorder Traversal 🌟🌟

Solution


106. Construct Binary Tree from Inorder and Postorder Traversal 🌟🌟

MUST READ


107. Binary Tree Level Order Traversal II 🌟🌟

Recursive solution

Iterative solution

Iterative Solution without reverse


110. Balanced Binary Tree 🌟🌟

Brute force

Optimized


112. Path Sum 🌟

O(N) Time , recursive

O(N) Time , iterative

soon…


116. Populating Next Right Pointers in Each Node 🌟🌟

This question is in continuation with A general approach to level order traversal questions series.

Previous Questions

  1. Binary tree level order traversal
  2. Binary tree level order traversal - II
  3. Binary tree zig-zag level order traversal
  4. Average of levels
  5. Binary tree right side view
  6. largest value in each tree row

Recursive Solution

Iterative Solution

O(N) Time and O(N) space

O(N) Time and O(1) space


118. Pascal’s Triangle 🌟

Straightforward solution

We can find any element(a[i][j]) in O(1) time using the formula (r-1)C(c-1) i.e (r-1)!/(c-1)!


119. Pascal’s Triangle II 🌟


120. Triangle 🌟🌟

Recursion(TLE)

Memoization (AC)

Dynamic Programming (AC)


121. Best Time to Buy and Sell Stock 🌟

O(N^2) Time and O(1) Space

O(N) Time and O(N) Space

O(N) Time and O(1) Space

Optimized inner loop : 33% less time.


122. Best Time to Buy and Sell Stock II 🌟🌟

Greedy - O(N) Time O(1) space (Valley-peak approach)

READ:


123. Best Time to Buy and Sell Stock III 🌟🌟🌟

Recursive Solution (TLE)

Memoization (AC)

Tabulation (AC)

Tabulation | space optimization (AC)


128. Longest Consecutive Sequence 🌟🌟

Sorting Solution

Hash table O(N) Time solution


129. Sum Root to Leaf Numbers 🌟🌟

O(N) Time recursive

Useful Comment:

  1. List of problems you need to master the concept :

If you find more problems, please comment it below :)


130. Surrounded Regions 🌟🌟

DFS Approach complex

DFS Approach more efficient


131. Palindrome Partitioning 🌟🌟

Backtracking solution


133. Clone Graph 🌟🌟

Solution

DFS Solution

BFS Solution


134. Gas Station 🌟🌟

Greedy Solution


136. Single Number 🌟

Sorting array

Using Map

Using XOR (^)


138. Copy List with Random Pointer 🌟🌟

Brute force

Optimized


141. Linked List Cycle 🌟

O(N) Time and O(N) space

O(N) Time and O(1) space - floyd’s cycle detection algorithm


142. Linked List Cycle II 🌟🌟

fast ans slow pointer Solution


143. Reorder List 🌟🌟

Most intuitive (Using stack)

Using Dequeue


144. Binary Tree Preorder Traversal 🌟

O(N) Time O(N) space (function call stack), Recursive

O(N) Time and O(N) extra space

Morris traversal - O(N) time and O(N) space.

Explanation soon…


145. Binary Tree Postorder Traversal 🌟

O(N) Time and O(2N) space, Iterative

NOTE: Here instead of *2 Stack* I have used *1 Stack and 1 vector* and reversed the vector at the end.

More detail explanation watch this 4 min video.

O(N) Time and O(N) Space, Iterative (1 stack)

This is bit tricky.Dry run 2-3 trees to understand Watch this Video.

O(N) Time and O(1) Space, Morris traversal

soon…


148. Sort List 🌟🌟

Intuitive Approach

merge sort


150. Evaluate Reverse Polish Notation 🌟🌟

Stack Solution


152. Maximum Product Subarray 🌟🌟

Brute Force

Kaden’s algorithm


155. Min Stack 🌟

TIP

Time Complexity: O(1) for all the solutions.

Space Complexity: Extra O(N) Space used.

Using 2 Vectors

Using 1 vector

Using vector of pair


165. Compare Version Numbers 🌟🌟

split and compare (two pass)


167. Two Sum II - Input array is sorted 🌟

O(N) Time 2-pointers solution


169. Majority Element 🌟

O(N^2) Brute force

O(N)Time with extra space

Moore’s Voting Algorithm


171. Excel Sheet Column Number 🌟

Intuitive Solution (AC)


188. Best Time to Buy and Sell Stock IV 🌟🌟🌟

Recursion (TLE)

Note that you could also set up the solution so that transactions are completed upon buying a stock instead.

Memoization (AC)

Tabulation (AC)


189. Rotate Array 🌟

O(N) Time and O(N) space

O(N) Time and O(1) Space


190. Reverse Bits 🌟

Using lsb


191. Number of 1 Bits 🌟

Using __builtin_popcount

Using n=n&(n-1) trick

READ:


198. House Robber 🌟🌟

Dynamic Programming

Reduced space complexity DP.

199. Binary Tree Right Side View 🌟🌟

This question is in continuation with A general approach to level order traversal questions series.

Previous Questions

  1. Binary tree level order traversal
  2. Binary tree level order traversal - II
  3. Binary tree zig-zag level order traversal
  4. Average of levels

Recursive Solution

Iterative Solution