This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DeepestLeftLeafNodeFinder { | |
Node deepestNode; | |
int deepestNodeDepth; | |
public Node findDeepestLeftLeafNode(Node root,boolean isLeftNode,int depth){ | |
if(isLeftNode){ | |
if(depth > deepestNodeDepth){ | |
deepestNode = root; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function which pushes all zeros to end of an array. | |
void pushZerosToEnd(int arr[], int n) | |
{ | |
int count = 0; // Count of non-zero elements | |
// Traverse the array. If element encountered is non-zero, then | |
// replace the element at index 'count' with this element | |
for (int i = 0; i < n; i++) | |
if (arr[i] != 0) | |
arr[count++] = arr[i]; // here count is incremented |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Quicksort can sort "inline" of an existing collection, e.g. it does not have to create a copy of the collection while Mergesort requires a copy. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileReader; | |
import java.io.InputStreamReader; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.util.StringTokenizer; | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.LinkedList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Step 1: Push the root node in the Stack. | |
Step 2: Loop until stack is empty. | |
Step 3: Peek the node of the stack. | |
Step 4: If the node has unvisited child nodes, get the unvisited child node, mark it as traversed and push it on stack. | |
Step 5: If the node does not have any unvisited child nodes, pop the node from the stack. | |
*/ | |
public void dfs() | |
{ | |
//DFS uses Stack data structure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Step 1: Push the root node in the Queue. | |
Step 2: Loop until the queue is empty. | |
Step 3: Remove the node from the Queue. | |
Step 4: If the removed node has unvisited child nodes, mark them as visited and insert the unvisited children in the queue. | |
*/ | |
public void bfs() | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<T> void mergeSort(T[] a, Comparator<T> c) { | |
if (a.length <= 1) return; | |
T[] a0 = Arrays.copyOfRange(a, 0, a.length/2); | |
T[] a1 = Arrays.copyOfRange(a, a.length/2, a.length); | |
mergeSort(a0, c); | |
mergeSort(a1, c); | |
merge(a0, a1, a, c); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Find kth smallest element in an array | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private boolean isCompoundWord(String word) { | |
if (dictionary.contains(word)) return true; | |
for (int i = 1; i < word.length(); i++) { | |
String prefix = word.substring(0, i); | |
if (isCompoundWord(prefix)) { | |
String remainder = word.substring(i, word.length()); | |
if (remainder.length() == 0) return true; | |
return isCompoundWord(remainder); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Integer findSum(int[] arr) { | |
if (arr.length == 0) | |
return null; | |
int largestSum = arr[0]; | |
int currentSum = arr[0]; | |
for (int i = 1; i < arr.length; i++) { | |
currentSum = max(currentSum + arr[i], arr[i]); // if the current sum gets negative, we reset it | |
largestSum = max(currentSum, largestSum); | |
} |