1、Given a BST (Binary search Tree) how will you find median in that?
Constraints:
* No extra memory.
* Function should be reentrant (No static, global variables allowed.)
* Median for even no of nodes will be the average of 2 middle elements and for odd no of terms will be middle element only.
* Algorithm should be efficient in terms of complexity.
思路:
1)中序遍历搜索二叉树得到有序的双向链表
2)采用快慢指针来寻找链表的中间元素
代码:
参考:http://blog.youkuaiyun.com/hhygcy/archive/2009/10/11/4654305.aspx