Skip to content

Instantly share code, notes, and snippets.

@zhangxin0804
zhangxin0804 / 4_4.java
Created July 4, 2015 20:51
CC150 4.4
/*
4.4
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 cc150Test;
import java.util.ArrayList;
import java.util.LinkedList;
/*
4.3
Given a sorted (increasing order) array with unique integer elements,
write an algorithm to create a binary search tree with minimal height.
*/
/*
解析:给定一个增序数组,其中包含的都是unique的整数。生成minimal height的BST。
@zhangxin0804
zhangxin0804 / 4_2.java
Last active August 29, 2015 14:24
CC150 4.2
/*
4.2
Given a directed graph, design an algorithm to find out whether there is a route between two nodes.
*/
package cc150Test;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
/*
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.
*/
// 时间复杂度: O(n), n为节点个数
// 空间复杂度: O(h), h为树高度。
@zhangxin0804
zhangxin0804 / 3_7.java
Created June 29, 2015 06:09
CC150 3.7
/*
3.7
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 they would like. Create the data structures to maintain this system and implement
operations such as enqueue, dequeueAny, dequeueDog and dequeueCat.You may use the built-in LinkedList data structure.
*/
/*