Skip to content

Instantly share code, notes, and snippets.

@WOLOHAHA
WOLOHAHA / CC150-4.6.java
Created July 22, 2014 12:56
Write an algorithm to find the 'next' node (i.e., in-order successor) of a given node in a binary search tree. You may assume that each node has a link to its parent.
package POJ;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main{
/**
*
@WOLOHAHA
WOLOHAHA / CC150-4.5.java
Created July 15, 2014 09:12
Implement a function to check if a binary tree is a binary search tree.
package POJ;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main{
/**
*
@WOLOHAHA
WOLOHAHA / CC150-4.4-1.java
Last active August 29, 2015 14:03
Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth (e.g., if you have a tree with depth D,you'll have D linked lists).
package POJ;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main{
/**
*
@WOLOHAHA
WOLOHAHA / CC150-4.3.java
Created July 13, 2014 13:29
Given a sorted (increasing order) array with unique integer elements, write an algorithm to create a binary search tree with minimal height.
import java.util.LinkedList;
import java.util.Queue;
public class Main{
/**
* 4.3 Given a sorted (increasing order) array with unique integer elements,
* write an algorithm to create a binary search tree with minimal height.
*
* @Runtime & spaces
@WOLOHAHA
WOLOHAHA / CC150-4.2-DirectedGraph.java
Last active August 29, 2015 14:03
Given a directed graph, design an algorithm to find out whether there is a route between two nodes.
package POJ;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class DirectedGraph {
private ArrayList<Integer>[] graph;
public DirectedGraph(int n){
graph=(ArrayList<Integer>[])new ArrayList[n];
for(int i=0;i<n;i++){
@WOLOHAHA
WOLOHAHA / CC150-4.1.java
Created July 12, 2014 08:48
Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.
package POJ;
public class Main{
/**
* 4.1 Implement a function to check if a binary tree is balanced. For the purposes
* of this question, a balanced tree is defined to be a tree such that the heights
* of the two subtrees of any node never differ by more than one.
*
* @Runtime & spaces
@WOLOHAHA
WOLOHAHA / CC150-3.7.java
Last active August 29, 2015 14:03
An animal shelter holds only dogs and cats, and operates on a strictly "first in, first out" basis. People must adOPT either the "oldest" (based on arrival time) of all animals at the shelter, or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type). They cannot select which specific animal th…
package POJ;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Main{
@WOLOHAHA
WOLOHAHA / CC150-3.6.java
Last active August 29, 2015 14:03
Write a program to sort a stack in ascending order (with biggest items on top). You may use at most one additional stack to hold items, but you may not copy the elements into any other data structure (such as an array).The stack supports the following operations: push, pop, peek, and isEmpty.
package POJ;
import java.util.Random;
import java.util.Stack;
public class Main{
/**
@WOLOHAHA
WOLOHAHA / CC150-3.5.java
Created July 6, 2014 16:39
Implement a MyQueue class which implements a queue using two stacks.
package POJ;
import java.util.ArrayList;
import java.util.Stack;
public class Main{
/**
*
@WOLOHAHA
WOLOHAHA / CC150-3.4.java
Last active January 9, 2024 08:12
In the classic problem of the Towers of Hanoi, you have 3 towers and Ndisks of different sizes which can slide onto any tower.The puzzle starts with disks sorted in ascending order of size from top to bottom (i.e., each disk sits on top of an even larger one). You have the following constraints: (1) Only one disk can be moved at a time. (2) A di…
package POJ;
import java.util.Stack;
public class Main{
/**
*
* 3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of