Skip to content

Instantly share code, notes, and snippets.

@zac-xin
zac-xin / PrintLevelOrder.java
Created November 15, 2012 03:01
print a binary tree in level order using BFS & DFS
/*
* print a binary tree in level order using DFS & BFS
*/
package Chapter4;
import java.util.Queue;
import java.util.LinkedList;
public class PrintLevelOrder {
public static void main(String args[]){
@zac-xin
zac-xin / FindLCA_2.java
Created November 9, 2012 03:04
Find LCA in Binary Tree
/*
* find lowest common ancestor in a Binary Tree
*/
package Chapter4;
public class FindLCA_2 {
public static void main(String args[]){
TreeNode n1 = new TreeNode(22, null, null);
TreeNode n2 = new TreeNode(2, n1, null);
TreeNode n4 = new TreeNode(4, null, null);
@zac-xin
zac-xin / FindLCA_1.java
Created November 9, 2012 03:00
Find LCA in BST
/*
* find lowest common ancestor in a Binary Search Tree(BST)
*/
package Chapter4;
public class FindLCA_1 {
public static void main(String args[]){
TreeNode n1 = new TreeNode(1, null, null);
TreeNode n2 = new TreeNode(2, n1, null);
TreeNode n4 = new TreeNode(4, null, null);
@zac-xin
zac-xin / Solution2.java
Created November 5, 2012 17:05
UniquePath1FromLeetcode
public class Solution {
public int uniquePaths(int m, int n) {
int matrix[][] = new int[m + 1][n + 1];
return backtrack(matrix, 0 , 0, m, n);
}
public int backtrack(int[][] matrix, int c, int r, int m, int n){
if(c == m - 1 && r == n -1){
return 1;
@zac-xin
zac-xin / UniquePath2.java
Created November 5, 2012 16:44
UniquePath2FromLeetcode
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int matrix[][] = new int[m][n];
for(int i = n - 1; i >= 0; i--){
@zac-xin
zac-xin / BuildTree.java
Created November 5, 2012 03:12
Build Tree From Inorder and Preorder
/*
* build tree from inorder and preorder
*/
package Chapter4;
public class BuildTree {
public static void main(String args[]){
int pre[] = {7, 10, 4, 3,1,2,8,11};
int in[] = {4,10,3,1,7,11,8,2};
TreeNode root =buildTree(pre, in);
@zac-xin
zac-xin / Solution.java
Created October 29, 2012 03:09
Permutation an array
import java.util.ArrayList;
public class Solution {
public ArrayList<ArrayList<Integer>> permute(int[] num) {
return permuteRecursive(num, 0);
}
public ArrayList<ArrayList<Integer>> permuteRecursive(int[] num, int index){
if(index == num.length - 1){
ArrayList<Integer> list = new ArrayList<Integer>();
@zac-xin
zac-xin / ReverseList.java
Created October 28, 2012 02:45
Reverse Linked List
package Chapter2;
/*
* reverse a linked list
*/
public class ReverseList {
public static void main(String args[]){
IntNode n1, n2, n3, n4, n5, n6,n7, n8, n9;
n9 = new IntNode(10, null);
n8 = new IntNode(9, n9);
n7 = new IntNode(8, n8);
package Chapter9;
/*
* 9-6 Given a matrix in which each row and each column is sorted,
* write a method to find an element in it.
*/
public class Question9_6 {
public static void main(String args[]){
int m[][]= {
{1,4,5,6,8},
{2,5,7,8,10},
/*
* Given a sorted array of strings which is interspersed with empty strings,
* write a meth- od to find the location of a given string.
Example: find “ball” in [“at”, “”, “”, “”, “ball”, “”, “”, “car”, “”, “”, “dad”, “”, “”]
will return 4
Example: find “ballcar” in [“at”, “”, “”, “”, “”, “ball”, “car”, “”, “”, “dad”, “”, “”]
will return -1
*/
package Chapter9;