Skip to content

Conversation

ashokdey
Copy link
Member

@ashokdey ashokdey commented Oct 10, 2019

🍂 What's Inside?

  • Find kth maximum in a BST
  • Find kth minimum in a BST

The solution is simple, return the inorder traversal of a BST which returns a sorted array (ascending order)

@ashokdey ashokdey requested a review from TheSTL October 10, 2019 10:53
@ashokdey ashokdey self-assigned this Oct 10, 2019

function findKthMax(rootNode, k) {
const arr = inOrderTraversal(rootNode);
return arr[arr.length - k];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if k > arr.length then throw error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 🎉

@ashokdey ashokdey changed the title New Problem: kth max in BST New Problem: kth max & min in BST Oct 10, 2019

function findKthMax(rootNode, k) {
const arr = inOrderTraversal(rootNode);
if (k < 0 || k > arr.lenth) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if k = 0
then return is undefined becuase arr[arr.length - 0 ]


function findKthMin(rootNode, k) {
const arr = inOrderTraversal(rootNode);
if (k < 0 || k > arr.lenth) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if k = 0
then return is undefined becuase arr[ 0 - 1 ]

Copy link
Member

@TheSTL TheSTL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashokdey 💯

@TheSTL TheSTL merged commit bfbee74 into master Oct 10, 2019
@ashokdey ashokdey deleted the problems branch October 13, 2019 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants