## xyz Interview Question for abcs

Country: India

Comment hidden because of low score. Click to expand.
1
of 1 vote

InOrder traversal. But counter starts incrementing after you reach the left most node (including it).
Return current node when the counter is equal to n.

Comment hidden because of low score. Click to expand.
0

This will work for BST only. But we have just the regular binary tree.

Comment hidden because of low score. Click to expand.
0

can plz u elaborate with example how ur algo works in binary tree (its not bst )

Comment hidden because of low score. Click to expand.
0

I think the author meant BST.
just "binary tree" is senseless, because he could just write "given an unsorted array".

Comment hidden because of low score. Click to expand.
0

I agree with zr.roman, additionally we can speedup the search if we can precompute and store the number of nodes in the left and right subtrees for each node

Comment hidden because of low score. Click to expand.
0
of 0 vote

Walk thru a tree and insert each element into a min heap.

Remove n elements from the heap to get the n-th smallest.

Comment hidden because of low score. Click to expand.
0
of 0 vote

Put all elements from the tree to a min heap. Remove n to get the nth smallest.

Comment hidden because of low score. Click to expand.
0
of 0 vote

using a max-heap (instead of a min-heap) is faster. insert the first n elements from the tree to the heap, then for each additional element, if it is higher than the top of the heap, pop the heap and push the new element. After inserting all elements, we have the nth smallest item on the top of the heap. The time complexity is k log n (where k = # items in tree) vs k log k.

Comment hidden because of low score. Click to expand.
0

Your solution seems clever, but it does not give any advantage, asymptotically speaking. here is why, your time complexity is: k log n, as max_heapify() takes log n for each (k - n) node. however, k log n is O(k).

if follow min-heap, it takes O(k) to build the min heap. And then, it will take n times log k to find the n-th smallest element, where each extraction of smallest element takes log k to max_heapify. So, it's k + n log k, which is still O(k).

but, as I said, asymptotically speaking ... you still have a great angel

Comment hidden because of low score. Click to expand.
0
of 0 vote

in case of arbitrary binary tree without any order of nodes the algorithm is the following:
1) re-arrange nodes to build a min-heap (in-place due to just re-arrangement of nodes). O(n).
2) k times call extract-min O(1) and min-heapify O( k log n).
Worst case is O(n log n) in case the kth smallest element is the greatest one by coincidence.

Comment hidden because of low score. Click to expand.
-1
of 1 vote

``````public int findKthSmallest(BinaryTreeNode<Integer> root, int k) {
if (null == root)
return -1;
int n = BinaryTree.size(root);
int a[] = new int[n];
inorder(root, a);
Arrays.sort(a);
return a[k - 1];
}

int i = 0;

public void inorder(BinaryTreeNode<Integer> root, int[] a) {
if (root != null) {
inorder(root.left, a);
a[i++] = root.data;
inorder(root.right, a);
}
}``````

Comment hidden because of low score. Click to expand.
-1
of 1 vote

Iterative version

``````public int kthSmallest(TreeNode root, int k) {
int h = 0;
Stack<TreeNode> stack = new Stack<>();
TreeNode current = root;
while (!stack.isEmpty() || current != null) {
if (current!= null) {
current = current.left;
}
else {
h += 1;
current = stack.pop();
if(h == k)
return current.val;
current = current.right;
}
}
return 0;
}``````

Comment hidden because of low score. Click to expand.
0

Given is binary tree not binary search tree. So, We do not have a way to tell inorder traversal give sorting sequence

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.