Leetcode

Leetcode

Tree traverse, DFS, BFS (BinaryTree traverse, preorder, inorder, postorder, bfs, bfs storing level)

Problems by type

DFS (Visit friends, numIslands, Valid binary search tree)

Sliding Window (minWindow, lengthOfLongestSubstring, checkInclusion)

BackTracking (subsets, combinationSum, permutations)

Dynamic (longestPalindrome, maxProfit, maxSubArray)

Tree traverse, DFS, BFS

DFS (Depth First Search): Pre, in and postorder are variations of DFS

Binary tree traverse

class BinaryNode1 {  
 
  public $data;  
  public $left;  
  public $right;  
 
  public function __construct(string $data = NULL) {  
	  $this->data = $data;  
	  $this->left = NULL;  
	  $this->right = NULL;  
  }  
 
  public function addChildren(BinaryNode1 $left, BinaryNode1 $right) {  
	  $this->left = $left;  
	  $this->right = $right;  
  }  
}  
 
class BinaryTree {  
 
  public $root = NULL;  
 
  public function __construct(BinaryNode1 $node) {  
  $this->root = $node;  
  }  
 
  public function traverse(BinaryNode1 $node, int $level = 0) 
  {  
  	  if ($node) {  
		  echo ("-", $level);  
		  echo $node->data . "\n";  
 
		  if ($node->left)  
		  $this->traverse($node->left, $level + 1);  
 
		  if ($node->right)  
		  $this->traverse($node->right, $level + 1);  
	  }  
 }  
}  

Binary tree DFS (pro, in, post)

DFS using stack:

  1. $stack[] = $root;

  2. $node = $stack->pop();

  3. Fetch its unvisited neighbours $stack->push($current->right); $stack->push($current->left);

  4. Repeat 2,3 until stack empty.

BFS traversal

enter image description here

DFS

Sliding Window

BackTracking

Backtracking is an algorithm for finding all solutions by exploring all potential candidates. If the solution candidate turns to be not a solution (or at least not the last one), backtracking algorithm discards it by making some changes on the previous step,

Dynamic Programming

Problems

1 Two Sum Ok

2 Add Two Numbers Ok

146 LRU Cache Ok

5 Longest Palindromic Substring Ok

4 Median of two sorted Arrays Ok

42 Trapping Rain Water Ok

200 Number of Islands Ok - DFS

3 Longest Subst wihout rep char Ok

15 3Sum Ok 23 Merge K Sorted Lists Ok

238 Product array execpt self Ok

76 MinWindow Substring OK - MWS

973 K Closest Points to Origin Ok - MinHeap y array

273 Integer to English Words Ok

301 Remove Invalid Parentheses Ok - Queue

289 Game of Life Ok - now in place

297 Serialize and Deserialize Binary T Ok - Queue y For

121 Best Time To Buy and Sell stock Ok

33 Search in rotated sorted Array Ok

253 Meeting Rooms Ok - Min Heap

22 Generate Parentheses Ok - Recursive

11 Container with most water Ok

49 Group Anagrams Ok

560 SubArray Sum Equals K Ok

380 Insert Delete GetRandom Ok

54 Spiral Matrix Ok

88 Merge Sorted Array Ok

56 Merge Intervals ¿? - Mejorar

Last updated