Hawstein's Blog

Make something people love

Cracking the coding interview--Q4.5

题目 原文: Write an algorithm to find the ‘next’ node (i.e., in-order successor) of a given node in a binary search tree where each node has a link to its parent. 译文: 给定二叉查找树的一个结点, 写一个算法查找它的“下一个”...

简明 STL 教程

前言 本文通过实例介绍STL的用法,没有长篇大论,没有深入源码,纯粹的“知其然”文章, 目的是看完这文章后即可使用STL中的容器及算法。STL提供了许多常用的数据结构及算法, 而且我相信其中许多的数据结构及算法都是CSer们学过的(比如栈,队列等), 使用STL可以避免每次都重新去实现它们(而且你的实现有可能很糟)。 前面已经说了,这只是一篇“知其然”的文章,如果想“知其所以然”, 推荐阅...

Cracking the coding interview--Q4.4

题目 原文: Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists). 译文: 给定一棵...

Cracking the coding interview--Q4.3

题目 原文: Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal height. 译文: 给定一个有序数组(递增),写程序构建一棵具有最小高度的二叉树。 解答 想要使构建出来的二叉树高度最小,那么对于任意结点, 它的左子树和右子树的结点数...

Cracking the coding interview--Q4.2

题目 原文: Given a directed graph, design an algorithm to find out whether there is a route between two nodes. 译文: 给定一个有向图,设计算法判断两结点间是否存在路径。 解答 根据题意,给定一个有向图和起点终点,判断从起点开始,是否存在一条路径可以到达终点。 考查的就是图的遍...

Cracking the coding interview--Q4.1

题目 原文: Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the ro...

Cracking the coding interview--Q3.6

题目 原文: Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implemented. The following are the only functions that should be used to wr...

Cracking the coding interview--Q3.5

题目 原文: Implement a MyQueue class which implements a queue using two stacks. 译文: 使用两个栈实现一个队列MyQueue。 解答 队列是先进先出的数据结构(FIFO),栈是先进后出的数据结构(FILO), 用两个栈来实现队列的最简单方式是:进入队列则往第一个栈压栈, 出队列则将第一个栈的数据依次压入第二个...

Cracking the coding interview--Q3.4

题目 原文: In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of si...

Cracking the coding interview--Q3.3

题目 原文: Imagine a (literal) stack of plates. If the stack gets too high, it might topple. Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshol...