Skip to content

Instantly share code, notes, and snippets.

@Lcjc
Lcjc / LCA_Not_In_Tree.java
Created November 21, 2017 05:16
Lowest Common Ancestor in Binary Tree (not guaranteed in tree)
/*
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
@Lcjc
Lcjc / InterleavePositiveAndNegativeElements.java
Last active September 27, 2017 19:49
Interleave Positive And Negative Elements
/*
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.
@Lcjc
Lcjc / mergeSort.java
Created September 21, 2017 18:05
Merge Sort (top-down implement)
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);
@Lcjc
Lcjc / quickSort.java
Last active December 29, 2020 04:27
Quick Sort (last element as pivot)
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