在二叉查找树的第n个最大元件 [英] Nth largest element in a binary search tree

查看:133
本文介绍了在二叉查找树的第n个最大元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何找到一个BST的第N个节点?

How to find the Nth largest node in a BST?

我总是一个计数变量,而这样做在BST中序遍历?返回元素时计数= N ???

Do I keep a count variable while doing In Order Traversal of a BST? Return the element when the count = N???

推荐答案

请参阅我的回答<一href="http://stackoverflow.com/questions/2329171/find-kth-smallest-element-in-a-binary-search-tree-in-optimum-way/2329236#2329236">here.您可以在做到这一点O(log n)的平均,其中n =节点数。最坏的情况是仍然 O(N)如果树不均衡(总是 O(log n)的如果不过是平衡的)。为了遍历总是 O(N)但是。

See my answer here. You can do this in O(log n) on average where n = number of nodes. Worst case is still O(n) IF the tree isn't balanced (always O(log n) if it is balanced however). In order traversal is always O(n) however.

这篇关于在二叉查找树的第n个最大元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆