Largest value in each level of Binary Tree
Given a binary tree, find the largest value in each level.
Examples :
Input :
1
/ \
2 3
Output : 1 3
Input :
4
/ \
9 2
/ \ \
3 5 7
Output : 4 9 7
Approach : The idea is to recursively traverse tree in a pre-order fashion. Root is considered to be at zeroth level. While traversing, keep track of the level of the element and if its current level is not equal to the number of elements present in the list, update the maximum element at that level in the list.
Below is the implementation to find largest value on each level of Binary Tree.
C++
// C++ program to find largest // value on each level of binary tree. #include <bits/stdc++.h> using namespace std; /* A binary tree node has data, pointer to left child and a pointer to right child */struct Node { int val; struct Node *left, *right; }; /* Recursive function to find the largest value on each level */void helper(vector<int>& res, Node* root, int d) { if (!root) return; // Expand list size if (d == res.size()) res.push_back(root->val); else // to ensure largest value // on level is being stored res[d] = max(res[d], root->val); // Recursively traverse left and // right subtrees in order to find // out the largest value on each level helper(res, root->left, d + 1); helper(res, root->right, d + 1); } // function to find largest values vector<int> largestValues(Node* root) { vector<int> res; helper(res, root, 0); return res; } /* Helper function that allocates a new node with the given data and NULL left and right pointers. */Node* newNode(int data) { Node* temp = new Node; temp->val = data; temp->left = temp->right = NULL; return temp; } // Driver code int main() { /* Let us construct a Binary Tree 4 / \ 9 2 / \ \ 3 5 7 */ Node* root = NULL; root = newNode(4); root->left = newNode(9); root->right = newNode(2); root->left->left = newNode(3); root->left->right = newNode(5); root->right->right = newNode(7); vector<int> res = largestValues(root); for (int i = 0; i < res.size(); i++) cout << res[i] << " "; return 0; } |
Java
// Java program to find largest // value on each level of binary tree. import java.util.*; class GFG { /* A binary tree node has data, pointer to left child and a pointer to right child */static class Node { int val; Node left, right; }; /* Recursive function to find the largest value on each level */static void helper(Vector<Integer> res, Node root, int d) { if (root == null) return; // Expand list size if (d == res.size()) res.add(root.val); else // to ensure largest value // on level is being stored res.set(d, Math.max(res.get(d), root.val)); // Recursively traverse left and // right subtrees in order to find // out the largest value on each level helper(res, root.left, d + 1); helper(res, root.right, d + 1); } // function to find largest values static Vector<Integer> largestValues(Node root) { Vector<Integer> res = new Vector<>(); helper(res, root, 0); return res; } /* Helper function that allocates a new node with the given data and NULL left and right pointers. */static Node newNode(int data) { Node temp = new Node(); temp.val = data; temp.left = temp.right = null; return temp; } // Driver code public static void main(String[] args) { /* Let us construct a Binary Tree 4 / \ 9 2 / \ \ 3 5 7 */ Node root = null; root = newNode(4); root.left = newNode(9); root.right = newNode(2); root.left.left = newNode(3); root.left.right = newNode(5); root.right.right = newNode(7); Vector<Integer> res = largestValues(root); for (int i = 0; i < res.size(); i++) System.out.print(res.get(i)+" "); } } /* This code is contributed by PrinciRaj1992 */ |
Python3
# Python program to find largest value # on each level of binary tree. """ Recursive function to find the largest value on each level """def helper(res, root, d): if ( not root): return # Expand list size if (d == len(res)): res.append(root.val) else: # to ensure largest value # on level is being stored res[d] = max(res[d], root.val) # Recursively traverse left and # right subtrees in order to find # out the largest value on each level helper(res, root.left, d + 1) helper(res, root.right, d + 1) # function to find largest values def largestValues(root): res = [] helper(res, root, 0) return res # Helper function that allocates a new # node with the given data and None left # and right pointers. class newNode: # Constructor to create a new node def __init__(self, data): self.val = data self.left = None self.right = None # Driver Code if __name__ == '__main__': """ Let us construct the following Tree 4 / \ 9 2 / \ \ 3 5 7 """ root = newNode(4) root.left = newNode(9) root.right = newNode(2) root.left.left = newNode(3) root.left.right = newNode(5) root.right.right = newNode(7) print(*largestValues(root)) # This code is contributed # Shubham Singh(SHUBHAMSINGH10) |
Output:
4 9 7
Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
Complexity Analysis:
- Time complexity : O(n), where n is the number of nodes in binary tree.
- Auxiliary Space : O(n) as in worst case, depth of binary tree will be n.
Recommended Posts:
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Difference between sums of odd level and even level nodes of a Binary Tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Get Level of a node in a Binary Tree
- Smallest value in each level of Binary Tree
- Height of binary tree considering even level leaves only
- Insertion in a Binary Tree in level order
- Swap Nodes in Binary tree of every k'th level
- Find maximum level sum in Binary Tree
- Density of Binary Tree using Level Order Traversal
- Get level of a node in binary tree | iterative approach
- Level Order Predecessor of a node in Binary Tree
- Level Order Successor of a node in Binary Tree
- Print nodes between two given level numbers of a binary tree
- Depth of the deepest odd level node in Binary Tree
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.



