# Top three elements in binary tree

We have a simple binary tree and we have to print the top 3 largest elements present in the binary tree.

Examples:

Input : 1 / \ 2 3 / \ / \ 4 5 4 5 Output :Three largest elements are 5 4 3

**Approach** We can simply take three variables first, second, third to store the first largest, second largest, third largest respectively and perform preorder traversal and each time we will update the elements accordingly.

This approach will take O(n) time only.

Algorithm-

1- Take 3 variables first, second, third 2- Perform a preorder traversal if (root==NULL) return if root's data is larger then first update third with second second with first first with root's data else if root's data is larger then second and not equal to first update third with second second with root's data else if root's data is larger then third and not equal to first & second update third with root's data 3- call preorder for root->left 4- call preorder for root->right

`// CPP program to find largest three elements in ` `// a binary tree. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `struct` `Node { ` ` ` `int` `data; ` ` ` `struct` `Node *left; ` ` ` `struct` `Node *right; ` `}; ` ` ` `/* Helper function that allocates a new Node with the ` ` ` `given data and NULL left and right pointers. */` `struct` `Node *newNode(` `int` `data) { ` ` ` `struct` `Node *node = ` `new` `Node; ` ` ` `node->data = data; ` ` ` `node->left = NULL; ` ` ` `node->right = NULL; ` ` ` `return` `(node); ` `} ` ` ` `// function to find three largest element ` `void` `threelargest(Node *root, ` `int` `&first, ` `int` `&second, ` ` ` `int` `&third) { ` ` ` `if` `(root == NULL) ` ` ` `return` `; ` ` ` ` ` `// if data is greater than first large number ` ` ` `// update the top three list ` ` ` `if` `(root->data > first) { ` ` ` `third = second; ` ` ` `second = first; ` ` ` `first = root->data; ` ` ` `} ` ` ` ` ` `// if data is greater than second large number ` ` ` `// and not equal to first update the bottom ` ` ` `// two list ` ` ` `else` `if` `(root->data > second && root->data != first) { ` ` ` `third = second; ` ` ` `second = root->data; ` ` ` `} ` ` ` ` ` `// if data is greater than third large number ` ` ` `// and not equal to first & second update the ` ` ` `// third highest list ` ` ` `else` `if` `(root->data > third && ` ` ` `root->data != first && ` ` ` `root->data != second) ` ` ` `third = root->data; ` ` ` ` ` `threelargest(root->left, first, second, third); ` ` ` `threelargest(root->right, first, second, third); ` `} ` ` ` `// driver function ` `int` `main() { ` ` ` `struct` `Node *root = newNode(1); ` ` ` `root->left = newNode(2); ` ` ` `root->right = newNode(3); ` ` ` `root->left->left = newNode(4); ` ` ` `root->left->right = newNode(5); ` ` ` `root->right->left = newNode(4); ` ` ` `root->right->right = newNode(5); ` ` ` ` ` `int` `first = 0, second = 0, third = 0; ` ` ` `threelargest(root, first, second, third); ` ` ` `cout << ` `"three largest elements are "` ` ` `<< first << ` `" "` `<< second << ` `" "` ` ` `<< third; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

three largest elements are 5 4 3

## Recommended Posts:

- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 1
- Check whether a binary tree is a full binary tree or not
- Check whether a given binary tree is skewed binary tree or not?
- Binary Tree to Binary Search Tree Conversion using STL set
- Check if a binary tree is subtree of another binary tree | Set 2
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Convert a given Binary tree to a tree that holds Logical AND property

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.