# Most Frequent Subtree Sum from a given Binary Tree

• Difficulty Level : Hard
• Last Updated : 07 Sep, 2021

Given a Binary Tree, the task is to find the most frequent subtree sum that can be obtained by considering every node of the given tree as the root of the subtree. If more than one such sums exist, then print all of them.

Examples:

Input:
5
/   \
2   -4

Output: 2 -4 3
Explanation:
The sum of nodes considering 5 as the root of subtree is 5 + 2 – 4 = 3.
The sum of nodes considering 2 as the root of subtree is 2 = 2.
The sum of nodes considering -4 as the root of subtree is -4 = -4.
Since all the sums have same frequency, print all the sum.

Input:
4
/   \
2    -4

Output: 2
Explanation:
The sum of nodes considering 4 as the root of subtree is 4 + 2 – 4 = 2.
The sum of nodes considering 2 as the root of subtree is 2 = 2.
The sum of nodes considering -4 as the root of subtree is -4 = -4.
Since, sum 2 has maximum frequency ( = 2). Hence, print the value 2.

Approach: The idea to use the DFS Traversal for the given tree to solve the given problem. Follow the below steps to solve the problem:

• Create two auxiliary Hash Map M and F where M is a set of integer keys and corresponding lists, and F will store the frequency of each number.
• Perform the DFS Traversal for the given tree and do the following:
• If the node is NULL, return 0.
• Initialize variables left and right that stores the value of the sum of nodes left and right subtree of the current node.
• Find the sum of currentNode.value + left + right store it in a variable totalSum.
• Now update the frequency of totalSum in the map F.
• Update the frequency of the value F[totalSum] as totalSum in the map M.
• Return the value to totalSum from the current recursive function.
• After the above steps, print all the element of the list M.rbegin().

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to print the vector``void` `printVector(vector<``int``> v)``{``    ``// Traverse vector c``    ``for` `(``int` `i = 0; i < v.size(); i++) {``        ``cout << v[i] << ``" "``;``    ``}``}` `// TreeNode class``class` `TreeNode {``public``:``    ``int` `val;``    ``TreeNode *left, *right;` `    ``// Constructor``    ``TreeNode(``int` `data)``    ``{``        ``val = data;``        ``left = NULL;``        ``right = NULL;``    ``}``};` `// Function to insert node in the``// binary tree``void` `insert(TreeNode** root, ``int` `val)``{``    ``// Initialize Queue``    ``queue q;` `    ``// Push the node root``    ``q.push(*root);` `    ``// While Q.size() is there``    ``while` `(q.size()) {` `        ``// Get the front node``        ``TreeNode* temp = q.front();``        ``q.pop();` `        ``// If left is NULL``        ``if` `(!temp->left) {``            ``if` `(val)``                ``temp->left = ``new` `TreeNode(val);``            ``else``                ``temp->left = ``new` `TreeNode(0);``            ``return``;``        ``}``        ``else` `{``            ``q.push(temp->left);``        ``}` `        ``// If right is NULL``        ``if` `(!temp->right) {``            ``if` `(val)``                ``temp->right = ``new` `TreeNode(val);``            ``else``                ``temp->right = ``new` `TreeNode(0);``            ``return``;``        ``}``        ``else` `{``            ``q.push(temp->right);``        ``}``    ``}``}` `// Function to make the tree from``// given node values``TreeNode* buildTree(vector<``int``> v)``{``    ``TreeNode* root = ``new` `TreeNode(v);` `    ``// Traverse and insert node``    ``for` `(``int` `i = 1; i < v.size(); i++) {``        ``insert(&root, v[i]);``    ``}``    ``return` `root;``}` `// Utility function to find subtree``// sum with highest frequency of a``// particular node``int` `findsubTreeSumUtil(``    ``TreeNode* node, map<``int``,``                        ``vector<``int``> >& mpp,``    ``map<``int``, ``int``>& frequency)``{``    ``if` `(!node)``        ``return` `0;` `    ``// Recur for the left subtree``    ``int` `left = findsubTreeSumUtil(``        ``node->left, mpp, frequency);` `    ``// Recur for the right subtree``    ``int` `right = findsubTreeSumUtil(``        ``node->right, mpp, frequency);` `    ``// Stores sum of nodes of a subtree``    ``int` `totalSum = node->val + left + right;` `    ``// Update the frequency``    ``if` `(!frequency.count(totalSum)) {``        ``mpp.push_back(totalSum);``        ``frequency[totalSum] = 1;``    ``}``    ``else` `{``        ``frequency[totalSum]++;``        ``mpp[frequency[totalSum]].push_back(totalSum);``    ``}` `    ``// Return the total sum``    ``return` `totalSum;``}` `// Function to find subtree sum with``// highest frequency of given tree``void` `findsubTreeSum(TreeNode* root)``{``    ``// Store list of nodes attached to``    ``// a particular node and frequency``    ``// of visited nodes``    ``map<``int``, vector<``int``> > mpp;``    ``map<``int``, ``int``> frequency;` `    ``// Base Case``    ``if` `(!root) {``        ``printVector({});``        ``return``;``    ``}` `    ``// DFS function call``    ``findsubTreeSumUtil(root, mpp, frequency);` `    ``// Print the vector``    ``printVector(mpp.rbegin()->second);``}` `// Driver Code``int` `main()``{``    ``// Given nodes of the tree``    ``vector<``int``> v = { 5, 2, -4 };` `    ``// Function call to build the tree``    ``TreeNode* tree = buildTree(v);` `    ``// Function Call``    ``findsubTreeSum(tree);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach` `// Importing required classes``import` `java.util.ArrayList;``import` `java.util.Arrays;``import` `java.util.HashMap;``import` `java.util.LinkedList;``import` `java.util.Queue;``import` `java.util.TreeMap;` `public` `class` `GFG {``    ``// Function to print the list``    ``static` `void` `printVector(ArrayList v)``    ``{``        ``// Traverse list``        ``for` `(``int` `i = ``0``; i < v.size(); i++) {``            ``System.out.print(v.get(i) + ``" "``);``        ``}``    ``}` `    ``// TreeNode class``    ``static` `class` `TreeNode {``        ``int` `val;``        ``TreeNode left, right;` `        ``// Constructor``        ``TreeNode(``int` `data)``        ``{``            ``val = data;``            ``left = ``null``;``            ``right = ``null``;``        ``}``    ``}` `    ``// Function to insert node in the``    ``// binary tree``    ``static` `void` `insert(TreeNode root, ``int` `val)``    ``{``        ``// Initialize Queue``        ``Queue q = ``new` `LinkedList();` `        ``// Push the node root``        ``q.add(root);` `        ``// While queue is not empty``        ``while` `(q.isEmpty() == ``false``) {` `            ``// Get the front node``            ``TreeNode temp = q.peek();``            ``q.poll();` `            ``// If left is NULL``            ``if` `(temp.left == ``null``) {``                ``temp.left = ``new` `TreeNode(val);``                ``return``;``            ``}``            ``else` `{``                ``q.add(temp.left);``            ``}` `            ``// If right is NULL``            ``if` `(temp.right == ``null``) {``                ``temp.right = ``new` `TreeNode(val);``                ``return``;``            ``}``            ``else` `{``                ``q.add(temp.right);``            ``}``        ``}``    ``}` `    ``// Function to make the tree from``    ``// given node values``    ``static` `TreeNode buildTree(ArrayList v)``    ``{``        ``TreeNode root = ``new` `TreeNode(v.get(``0``));` `        ``// Traverse and insert node``        ``for` `(``int` `i = ``1``; i < v.size(); i++) {``            ``insert(root, v.get(i));``        ``}``        ``return` `root;``    ``}` `    ``// Utility function to find subtree``    ``// sum with highest frequency of a``    ``// particular node``    ``static` `int` `findsubTreeSumUtil(``        ``TreeNode node,``        ``TreeMap > mpp,``        ``HashMap frequency)``    ``{``        ``if` `(node == ``null``)``            ``return` `0``;` `        ``// Recur for the left subtree``        ``int` `left``            ``= findsubTreeSumUtil(node.left, mpp, frequency);` `        ``// Recur for the right subtree``        ``int` `right = findsubTreeSumUtil(node.right, mpp,``                                       ``frequency);` `        ``// Stores sum of nodes of a subtree``        ``int` `totalSum = node.val + left + right;` `        ``// Update the frequency``        ``// If there is no node with sub-tree sum equal to``        ``// totalSum It is the first occurrence of totalSum``        ``// value``        ``if` `(frequency.get(totalSum) == ``null``) {``            ``ArrayList temp = mpp.get(``1``);``            ``// If there is no sum which has occurred only``            ``// once``            ``if` `(temp == ``null``)``                ``temp = ``new` `ArrayList();``            ``temp.add(totalSum);``            ``mpp.put(``1``, temp);``            ``frequency.put(totalSum, ``1``);``        ``}``        ``// There is a node with sub-tree sum equal to``        ``// totalSum``        ``else` `{``            ``frequency.put(totalSum,``                          ``frequency.get(totalSum) + ``1``);``            ``// Get list of sum values which has``            ``// occurrence equal to occurrence of totalSum``            ``ArrayList temp``                ``= mpp.get(frequency.get(totalSum));``            ``// If there is no sum which has occurrence``            ``// equal to occurrence of totalSum``            ``if` `(temp == ``null``)``                ``temp = ``new` `ArrayList();``            ``temp.add(totalSum);``            ``mpp.put(frequency.get(totalSum), temp);``        ``}` `        ``// Return the total sum``        ``return` `totalSum;``    ``}` `    ``// Function to find subtree sum with``    ``// highest frequency of given tree``    ``static` `void` `findsubTreeSum(TreeNode root)``    ``{``        ``// Store list of nodes attached to``        ``// a particular node and frequency``        ``// of visited nodes``        ``TreeMap > mpp``            ``= ``new` `TreeMap >();``        ``HashMap frequency``            ``= ``new` `HashMap();` `        ``// Base Case``        ``if` `(root == ``null``) {``            ``return``;``        ``}` `        ``// DFS function call``        ``findsubTreeSumUtil(root, mpp, frequency);` `        ``// Print the list of most-frequent subtree sum``        ``printVector(mpp.lastEntry().getValue());``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String args[])``    ``{``        ``// Given nodes of the tree``        ``ArrayList v = ``new` `ArrayList();``        ``v.addAll(Arrays.asList(``5``, ``2``, -``4``));` `        ``// Function call to build the tree``        ``TreeNode tree = buildTree(v);` `        ``// Function Call``        ``findsubTreeSum(tree);``    ``}``}` `// This code is contributed by jainlovely450`

Output:

`2 -4 3`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up