• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

K’th Largest Element in BST when modification to BST is not allowed

March 19, 2015 by Dhaval Dave

Given a Binary Search Tree (BST) and a positive integer k, find the k’th largest element in the Binary Search Tree.

      [20]
      /       
    [8]  [22] 
    /  \
  [5]  [10]
       /  \
     [9]  [12]

For example, in the following BST, if
if k = 3, then output should be 12
if k = 5, then output should be 9.

Method 1)  Brute Force in O(n)

In order of BST gives 5,8,9,10,12,20,22
and print member A[n-k+1] will be out put.

Now Lets discuss tricky way :

As we know that larger elements are in Right Side of a node and smaller in left side for BST.
Can we use this trick ???

Think for above example , R=no if Recursion,
r = 1 we traverse 22 and found that its largest
r = 2 we traverse 20, which is second largest.
r=3 and we went to 12 via 8-10-12 ( first traverse right path always)
so 3rd largest is 12.

So with Post Order Traversal We can find Kth largest in O(k + h) time

So Method 2 ) Post Order Traversal 

Algo . C=Count of node visited, K= Kth number

Function KthLargest(Node *root, int k, int &c)

  1. If root=null or c>= k
    return.
  2. KthLargest ( root->right ,  k, c)
  3. c++
  4. if ( c == k) cout << root -> n;
  5. KthLargest ( root->left ,  k, c)

For queries, suggestion and more questions / solutions please write us at admin@gohired.in

Similar Articles

Filed Under: Amazon Interview Question, Flipkart Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Binary Search Tree

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Templates in C++

Find min element in Sorted Rotated Array (Without Duplicates)

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Implement LRU Cache

Check if an array has duplicate numbers in O(n) time and O(1) space

C++ OOPs Part2

Generate largest number arranging a no. of given non negative integer numbers

CodeChef’ RRCOPY

Maximum sum contiguous subarray of an Array

Convert Decimal to Roman numbers / Romanizer HackerEarth Code

Get Minimum element in O(1) from input numbers or Stack

Python Dictionaries

Handle duplicates in Binary Search Tree

Find Nearest Minimum number in left side in O(n)

Find the number ABCD such that when multipled by 4 gives DCBA.

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

How strtok() Works

Stock Buy Sell to Maximize Profit

Printing Longest Common Subsequence

Linked List V/S Binary Search Tree

Binary Tree in Java

25 horses 5 tracks Find 3 fastest puzzle

Reverse a Linked List in groups of given size

Find the element that appears once others appears thrice

BlueStone E-commerce Interview Experience

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

Inorder and Preorder traversals of a Binary Tree given. Output the Postorder traversal of it.

DFS (Depth First Search)

Given a float number convert it into the string WITHOUT using any inbuilt Function

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Copyright © 2025 · Genesis Framework · WordPress · Log in