Find Kth minimum node in a binary tree and suggest a complexity
Antwoorden op sollicitatievragen
Anoniem
12 mei 2016
Do a preorder traversal of the tree which is O(n). Then use a pivot quicksort method to find the kth index which is K(log(n)). So the total runtime is O(n +K(log(n)))
1
Anoniem
3 nov 2015
Convert the bin tree into a BST [O(n)]. Traverse k elements of the BST [O(k log n)]. Total O(n+k log n).
Anoniem
28 jun 2015
-- Convert the binary tree into a MinHeap binary tree from the nodes
-- delete K nodes from the MinHeap binary tree.
Complexity :
Time required to construct the Min heap by an optimized way: n
Time required to delete K nodes from the binary tree = O(K log n)
Total complexity : O(n + K log n)
Another solution:
-- parse the whole tree and find and store the node with the minumum value and delete that node.
-- repeat the exercise for K times and the value found in K th parse is the intended value
Total complexity : O(Kn)
Anoniem
6 sep 2015
Supposing a BST, where the Node struct is expanded with "int size" which defines the size of its subtree included itself:
public Node fintMinimum(Node root, int k)
{
if (root == null) return null;
if (k = k) {
return findMinimum(root.left, k);
}
return findMinimum(root.right, k-(root.left.size+1));
}
}