具有大多数节点的二叉树中的级别

时间:2015-05-06 17:06:30

标签: algorithm data-structures

找出二叉树中哪个级别随机或BST )具有所需的最小空间量是多少?大多数节点数?

2 个答案:

答案 0 :(得分:1)

If you are allowed to destroy the tree, then you can convert the tree to a linked list while doing a bfs of the tree, essentially simulating a queue with the tree itself!

You can find information about that here: Convert a binary tree to linked list, breadth first, constant storage/destructive

中显示所有字符

这只需要O(1)空间,因为您已经重用了树的节点。

答案 1 :(得分:0)

O(1)

以广度优先搜索方法遍历BT(二叉树)。推送节点并提及其级别。您将遍历某个级别中的所有节点,然后转到下一级别。所以只需保持最大变量并不断更新它。

队列(针对BST)可以在O(2^(log(n) -1))中占用空间。

相关问题