Convert a BST to max heap without using extra memory and in as optimum time as possible
-----------------------------------------------------------------
/* Bottom Up recursive code.
* Basic Idea: Swap the right child to its parent bottom up to make it a Max Heap.
*/
NODE* BSTMaxHeapify(NODE* Tree)
{
if(Tree==NULL)
return Tree;
BSTMaxHeapify(Tree->left);
BSTMaxHeapify(Tree->right);
return(BSTMaxHeapSwap(Tree,Tree->right));
}
NODE* BSTMaxHeapSwap(NODE* Root, NODE* RootRight)
{
if(RootRight==NULL)
return Root;
else
Swap the Root and RootRight Node;
return RootRight;
}
Plz comment if I have made any mistake
BST转最大堆

本文介绍了一种不使用额外内存且尽可能优化时间复杂度的方法,将二叉搜索树(BST)转换为最大堆。通过递归地调整树的结构,确保每个节点的值都大于其子节点的值。
7万+

被折叠的 条评论
为什么被折叠?



