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
/* | |
Lowest Common Ancestor in Binary Tree (not guaranteed in tree) | |
Given two nodes in a binary tree, find their lowest common ancestor (the given two nodes are not guaranteed to be in the binary tree). | |
Return null If any of the nodes is not in the tree. | |
There is no parent pointer for the nodes in the binary tree | |
The given two nodes are not guaranteed to be in the binary tree | |
Example |
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
/* | |
Given an array with both positive and negative numbers in random order. | |
Shuffle the array so that positive and negative numbers are put in position with even and odd indices, respectively. | |
If there are more positive/negative numbers, put them at the end of the array. | |
The ordering of positive/negative numbers does not matter. | |
Assumptions: | |
The given array is not null. |
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 Solution { | |
public int[] mergeSort(int[] array) { | |
//corner and base cases | |
if(array == null || array.length <= 1) return array; | |
//separate the original array into two halves | |
int half = array.length/2; | |
int[] left = Arrays.copyOfRange(array, 0, half); | |
int[] right = Arrays.copyOfRange(array, half, array.length); | |
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 Solution { | |
public int[] quickSort(int[] array) { | |
//corner cases | |
if(array == null) return array; | |
quickSort2(array, 0, array.length-1); | |
return array; | |
} | |
//quicksort recursive method |