Pergunta de entrevista da empresa Meta

Find Kth minimum node in a binary tree and suggest a complexity

Respostas da entrevista

Sigiloso

12 de mai. de 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

Sigiloso

3 de nov. de 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).

Sigiloso

28 de jun. de 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)

Sigiloso

6 de set. de 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)); } }