<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>second largest element in generic tree java - Wikitechy</title>
	<atom:link href="https://www.wikitechy.com/interview-questions/tag/second-largest-element-in-generic-tree-java/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.wikitechy.com/interview-questions/tag/second-largest-element-in-generic-tree-java/</link>
	<description>Interview Questions</description>
	<lastBuildDate>Mon, 13 Sep 2021 07:23:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.wikitechy.com/interview-questions/wp-content/uploads/2025/10/cropped-wikitechy-icon-32x32.png</url>
	<title>second largest element in generic tree java - Wikitechy</title>
	<link>https://www.wikitechy.com/interview-questions/tag/second-largest-element-in-generic-tree-java/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to find Second largest element in BST ?</title>
		<link>https://www.wikitechy.com/interview-questions/data-structure/how-to-find-second-largest-element-in-bst/</link>
					<comments>https://www.wikitechy.com/interview-questions/data-structure/how-to-find-second-largest-element-in-bst/#respond</comments>
		
		<dc:creator><![CDATA[Editor]]></dc:creator>
		<pubDate>Tue, 13 Jul 2021 11:24:08 +0000</pubDate>
				<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[Accenture interview questions and answers]]></category>
		<category><![CDATA[Altimetrik India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Applied Materials interview questions and answers]]></category>
		<category><![CDATA[Bharti Airtel interview questions and answers]]></category>
		<category><![CDATA[BMC Software interview questions and answers]]></category>
		<category><![CDATA[Capgemini interview questions and answers]]></category>
		<category><![CDATA[CASTING NETWORKS INDIA PVT LIMITED interview questions and answers]]></category>
		<category><![CDATA[CGI Group Inc interview questions and answers]]></category>
		<category><![CDATA[Chetu interview questions and answers]]></category>
		<category><![CDATA[Ciena Corporation interview questions and answers]]></category>
		<category><![CDATA[Collabera Te interview questions and answers]]></category>
		<category><![CDATA[Dell International Services India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[find kth largest element in a binary search tree java]]></category>
		<category><![CDATA[find max value in binary search tree c++]]></category>
		<category><![CDATA[find maximum in binary search tree]]></category>
		<category><![CDATA[find min and max in binary search tree in c]]></category>
		<category><![CDATA[find the 2nd-largest node in a binary tree]]></category>
		<category><![CDATA[find the node with maximum value in a binary search tree]]></category>
		<category><![CDATA[find the second largest element in a binary search tree java]]></category>
		<category><![CDATA[find the second largest element in a binary search tree python]]></category>
		<category><![CDATA[Flipkart interview questions and answers]]></category>
		<category><![CDATA[geekyants interview questions and answers]]></category>
		<category><![CDATA[Genpact interview questions and answers]]></category>
		<category><![CDATA[Globallogic India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[IBM interview questions and answers]]></category>
		<category><![CDATA[Indecomm Global Services interview questions and answers]]></category>
		<category><![CDATA[kth largest element in a stream bst]]></category>
		<category><![CDATA[kth largest element in bst]]></category>
		<category><![CDATA[kth smallest element in a bst leetcode]]></category>
		<category><![CDATA[kth smallest element in a bst python]]></category>
		<category><![CDATA[kth smallest element in a bst recursive]]></category>
		<category><![CDATA[largest node in bst]]></category>
		<category><![CDATA[largest number in bst which is less than or equal to n java]]></category>
		<category><![CDATA[Mphasis interview questions and answers]]></category>
		<category><![CDATA[NetApp interview questions and answers]]></category>
		<category><![CDATA[Oracle Corporation interview questions and answers]]></category>
		<category><![CDATA[SAP Labs India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Sapient Consulting Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[second largest element in bst]]></category>
		<category><![CDATA[second largest element in bst java]]></category>
		<category><![CDATA[second largest element in bst leetcode]]></category>
		<category><![CDATA[second largest element in bst python]]></category>
		<category><![CDATA[second largest element in generic tree]]></category>
		<category><![CDATA[second largest element in generic tree java]]></category>
		<category><![CDATA[second largest element in n ary tree java]]></category>
		<category><![CDATA[second largest element in tree java]]></category>
		<category><![CDATA[second largest in tree]]></category>
		<category><![CDATA[second minimum node in a binary tree]]></category>
		<category><![CDATA[Tech Mahindra interview questions and answers]]></category>
		<category><![CDATA[Tracxn Technologies Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[UnitedHealth Group interview questions and answers]]></category>
		<category><![CDATA[Wipro Infotech interview questions and answers]]></category>
		<category><![CDATA[WM Global Technology Services India Pvt.Ltd Limited (WMGTS) interview questions and answers]]></category>
		<category><![CDATA[Xoriant Solutions Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Yodlee Infotech Pvt Ltd interview questions and answers]]></category>
		<guid isPermaLink="false">https://www.wikitechy.com/interview-questions/?p=480</guid>

					<description><![CDATA[Answer : In an N-ary tree, the second largest value in the given tree to find and return the node...]]></description>
										<content:encoded><![CDATA[<div class="TextHeading">
<div class="hddn">
<h2 id="how-to-find-second-largest-element-in-bst" class="color-pink" style="text-align: justify;">How to find Second largest element in BST ?</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>In an N-ary <a href="https://www.wikitechy.com/tutorials/r-programming/r-decision-tree" target="_blank" rel="noopener">tree</a>, the second largest value in the given tree to find and return the node. Return NULL if no node with required value is present.</li>
</ul>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>For example, in the given tree</li>
</ul>
</div>
</div>
<div class="ImageContent" style="text-align: justify;">
<div class="hddn"><img decoding="async" class="img-responsive center-block aligncenter" src="https://cdn.wikitechy.com/interview-questions/data-structure/how-to-find-second-largest-element-in-a-bst.png" alt="Second largest element in a BST" /></div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<h3 id="second-largest-node-is-20">Second largest node is 20.</h3>
</div>
</div>
<div class="Content">
<div class="hddn">
<ul style="text-align: justify;">
<li>A simple solution is to traverse the <a href="https://www.wikitechy.com/tutorials/c++/passing-2d-array-to-function-c++" target="_blank" rel="noopener">array</a> twice. In the first traversal the maximum value node to be find.</li>
<li>In the second traversal find the greatest element node less than the element obtained in first traversal.</li>
<li>This solution O(n) is the <a href="https://www.wikitechy.com/interview-questions/programming/what-is-the-time-complexity-of-adding-three-matrices" target="_blank" rel="noopener">time complexity</a>.</li>
<li>To find the second largest element in a single traversal to be efficient solution.</li>
</ul>
<h2 id="algorithm" class="color-green" style="text-align: justify;">Algorithm</h2>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-markdown code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-markdown code-embed-code">Initialize two nodes first and second to NULL as,<br/>first = second = NULL<br/><br/>Start traversing the tree,<br/>If the current node data say root-&gt;key is greater<br/>than first-&gt;key then update first and second as,<br/>second = first<br/>first = root<br/>If the current node data is in between first and<br/>second, then update second to store the value<br/>of current node as<br/>second = root<br/><br/>Return the node stored in second.</code></pre> </div>
<h2 id="sample-code-in-java" class="color-blue" style="text-align: justify;">Sample Code in Java</h2>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">// Java code to find second largest element in BST <br/>  <br/>// A binary tree node <br/>class Node { <br/>  <br/>    int data; <br/>    Node left, right; <br/>  <br/>    Node(int d) <br/>    { <br/>        data = d; <br/>        left = right = null; <br/>    } <br/>} <br/>  <br/>class BinarySearchTree { <br/>  <br/>    // Root of BST <br/>    Node root; <br/>  <br/>    // Constructor <br/>    BinarySearchTree() <br/>    { <br/>        root = null; <br/>    } <br/>  <br/>    // function to insert new nodes <br/>    public void insert(int data) <br/>    { <br/>        this.root = this.insertRec(this.root, data); <br/>    } <br/>      <br/>    /* A utility function to insert a new node with given  <br/>    key in BST */<br/>    Node insertRec(Node node, int data) <br/>    { <br/>        /* If the tree is empty, return a new node */<br/>        if (node == null) { <br/>            this.root = new Node(data); <br/>            return this.root; <br/>        } <br/>  <br/>        /* Otherwise, recur down the tree */<br/>        if (data &lt; node.data) { <br/>            node.left = this.insertRec(node.left, data); <br/>        } else { <br/>            node.right = this.insertRec(node.right, data); <br/>        } <br/>        return node; <br/>    } <br/>  <br/>    // class that stores the value of count <br/>    public class count { <br/>        int c = 0; <br/>    } <br/>  <br/>    // Function to find 2nd largest element <br/>    void secondLargestUtil(Node node, count C) <br/>    {    <br/>        // Base cases, the second condition is important to <br/>        // avoid unnecessary recursive calls <br/>        if (node == null || C.c &gt;= 2) <br/>            return; <br/>              <br/>        // Follow reverse inorder traversal so that the <br/>        // largest element is visited first <br/>        this.secondLargestUtil(node.right, C);  <br/>          <br/>         // Increment count of visited nodes <br/>        C.c++; <br/>          <br/>        // If c becomes k now, then this is the 2nd largest <br/>        if (C.c == 2) { <br/>            System.out.print(&quot;2nd largest element is &quot;+ <br/>                                              node.data); <br/>            return; <br/>        } <br/>          <br/>         // Recur for left subtree <br/>        this.secondLargestUtil(node.left, C);  <br/>    } <br/>  <br/>    // Function to find 2nd largest element <br/>    void secondLargest(Node node) <br/>    {    <br/>        // object of class count <br/>        count C = new count();  <br/>        this.secondLargestUtil(this.root, C); <br/>    } <br/>  <br/>    // Driver function <br/>    public static void main(String[] args) <br/>    { <br/>        BinarySearchTree tree = new BinarySearchTree(); <br/>          <br/>        /* Let us create following BST <br/>              50 <br/>           /     \ <br/>          30      70 <br/>         /  \    /  \ <br/>       20   40  60   80 */<br/>         <br/>        tree.insert(50); <br/>        tree.insert(30); <br/>        tree.insert(20); <br/>        tree.insert(40); <br/>        tree.insert(70); <br/>        tree.insert(60); <br/>        tree.insert(80); <br/>  <br/>        tree.secondLargest(tree.root); <br/>    } <br/>} </code></pre> </div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="output" class="color-blue">Output</h2>
</div>
</div>
<div class="Output" style="text-align: justify;">
<div class="hddn">
<figure class="highlight">
<pre><code class="hljs" data-lang=""><span class="nt">2nd largest element is 70
</span></code></pre>
</figure>
</div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="time-complexity" class="color-purple">Time Complexity</h2>
</div>
</div>
<div class="Content">
<div class="hddn">
<ul>
<li style="text-align: justify;">Time Complexity: The above solution is O(h) where h is height of <a href="https://www.wikitechy.com/technology/java-program-check-binary-tree-bst-not/" target="_blank" rel="noopener">BST</a>.</li>
</ul>
</div>
</div>
</div>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.wikitechy.com/interview-questions/data-structure/how-to-find-second-largest-element-in-bst/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
