算法题收藏

来源:互联网 发布:php exec bat 编辑:程序博客网 时间:2024/05/20 06:23

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.csdn.net/hhygcy/archive/2009/10/11/4654305.aspx

原创粉丝点击