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
/* | |
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; |
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
/* | |
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。 |
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
/* | |
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; |
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
/* | |
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为树高度。 |
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
/* | |
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. | |
*/ | |
/* |