Given a modified binary search tree, find k'th smallest element

Suppose in a given binary tree , if each node contains the number of children , then what is the best way to find the k'th smallest element in the tree?

Please note that this is not an ordinary BST. Each node contains the number of a child below it.

+4
source share
3 answers
find_element(root, k) if(root.left.nchildren + 1 == k - 1) return root; if(root.left.nchildren + 1 >= k) return find_element(root.left, k) else return find_element(root.right, k - (root.left.children + 1)) 
+4
source

Here is what I got:

 find (root, k) { leftChildCount = root->left->n rightChildCount = root->right->n if (leftChildCount+1 == k) Print root node else if (k< leftChildCount) Find(root->left,k) else Find(root->right,k-leftChildCount) } 
0
source

Move BST to InOrder , and save the elements of the array. Your array is a sorted array.

0
source

All Articles