You are not logged in. Please login at www.codechef.com to post your questions!

×

Second largest element in a binary search tree.

How to find out the second largest largest element in a binary search tree?

asked 12 Nov '16, 07:54

rashedcs's gravatar image

1★rashedcs
4871525
accept rate: 4%


void print2ndLargest ( node * root) { if(root==NULL||(root->Rchild==NULL&&root->Lchild==NULL)) { cout<<"2nd Largest Not possible \n"; return; } if(root->Rchild==NULL) { node * temp; temp=root->Lchild; while(temp->Rchild!=NULL) { temp=temp->Rchild; } cout<<"2nd Largest : "<< temp->info; return; } node * parentt; node * temp; parentt=root; temp=root->Rchild; while(temp->Rchild!=NULL) { parentt=temp; temp=temp->Rchild; } cout<< "2nd Largest : " << parentt->info; //Largest is : temp->info }

^ Hope this helps.

link

answered 12 Nov '16, 17:26

vikasj554's gravatar image

4★vikasj554
2097
accept rate: 33%

If a BST have atleast 2 nodes then... Second largest Element in a BST is the parent of Largest Element of BST. ie its the parent of rightmost node. If a BST don't have right subtree Second largest element is the rightmost child of left subtree.

link

answered 12 Nov '16, 09:19

chandyshot's gravatar image

3★chandyshot
2388
accept rate: 20%

edited 12 Nov '16, 17:33

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×773

question asked: 12 Nov '16, 07:54

question was seen: 1,075 times

last updated: 18 Nov '16, 21:53