# Second Largest element in n-ary tree

Given an N-ary tree, find and return the node with second largest value in the given tree. Return NULL if no node with required value is present.

For example, in the given tree Second largest node is 20.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to traverse the array twice. In the first traversal find the maximum value node. In the second traversal find the greatest element node less than the element obtained in first traversal. The time complexity of this solution is O(n).

An Efficient Solution can be to find the second largest element in a single traversal.
Below is the complete algorithm for doing this:

```1) Initialize two nodes first and second to NULL as,
first = second = NULL
2) Start traversing the tree,
a) If the current node data say root->key is greater
than first->key then update first and second as,
second = first
first = root
b) If the current node data is in between first and
second, then update second to store the value
of current node as
second = root
3) Return the node stored in second.
```
 `// CPP program to find second largest node ` `// in an n-ary tree. ` `#include ` `using` `namespace` `std; ` ` `  `// Structure of a node of an n-ary tree ` `struct` `Node { ` `    ``int` `key; ` `    ``vector child; ` `}; ` ` `  `// Utility function to create a new tree node ` `Node* newNode(``int` `key) ` `{ ` `    ``Node* temp = ``new` `Node; ` `    ``temp->key = key; ` `    ``return` `temp; ` `} ` ` `  `void` `secondLargestUtil(Node* root, Node** first, ` `                       ``Node** second) ` `{ ` `    ``if` `(root == NULL) ` `        ``return``; ` ` `  `    ``// If first is NULL, make root equal to first ` `    ``if` `(!(*first)) ` `        ``*first = root; ` ` `  `    ``// if root is greater than first then second ` `    ``// will become first and update first equal ` `    ``// to root ` `    ``else` `if` `(root->key > (*first)->key) { ` `        ``*second = *first; ` `        ``*first = root; ` `    ``} ` ` `  `    ``// If root is less than first but greater than ` `    ``// second ` `    ``else` `if` `(!(*second) || root->key > (*second)->key) ` `        ``*second = root; ` ` `  `    ``// number of children of root ` `    ``int` `numChildren = root->child.size(); ` ` `  `    ``// recursively calling for every child ` `    ``for` `(``int` `i = 0; i < numChildren; i++) ` `        ``secondLargestUtil(root->child[i], first, second); ` `} ` ` `  `Node* secondLargest(Node* root) ` `{ ` `    ``// second will store the second highest value ` `    ``Node* second = NULL; ` ` `  `    ``// first will store the largest value in the tree ` `    ``Node* first = NULL; ` ` `  `    ``// calling the helper function ` `    ``secondLargestUtil(root, &first, &second); ` ` `  `    ``if` `(second == NULL) ` `        ``return` `NULL; ` ` `  `    ``// This handles the case when every element in tree are same. ` `    ``if` `(first->key == second->key) ` `        ``return` `NULL; ` ` `  `    ``// return the second largest element ` `    ``return` `second; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``/*   Let us create below tree ` `   ``*             5 ` `   ``*         /   |  ` `   ``*         1   2   3 ` `   ``*        /   /    ` `   ``*       15  4   5   6 ` `   ``*/` ` `  `    ``Node* root = newNode(5); ` `    ``(root->child).push_back(newNode(1)); ` `    ``(root->child).push_back(newNode(2)); ` `    ``(root->child).push_back(newNode(3)); ` `    ``(root->child->child).push_back(newNode(15)); ` `    ``(root->child->child).push_back(newNode(4)); ` `    ``(root->child->child).push_back(newNode(5)); ` `    ``(root->child->child).push_back(newNode(6)); ` ` `  `    ``if` `(secondLargest(root) != NULL) ` `        ``cout << ``"Second largest element is : "` `<< secondLargest(root)->key << endl; ` `    ``else` `        ``cout << ````"Second largest element not found "````; ` ` `  `    ``return` `0; ` `} `

Output:

```Second largest element is : 6
```

## tags:

Tree n-ary-tree Tree