top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Given a binary tree (not BST), we are supposed to find nth smallest element?

+2 votes
475 views
Given a binary tree (not BST), we are supposed to find nth smallest element?
posted Feb 11, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Algorithm to find nth smallest element in Binary Tree(Not BST)

  1. Take dynamic Array(in c++/java)
  2. Traverse all the elements of The given Binary Tree using any traversal algorithm (inorder, preorder, postorder, levelorder method) and keep each element in dynamic array.
  3. Sort dynamic Array in non decreasing order.
  4. choose nth element in Array i.e Array[n].

Time complexity=O(n)+O(nlogn)+O(1)=O(nlogn).

answer Feb 11, 2016 by Rajan Paswan
What is levelorder traversal?
...