- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find largest subtree sum in a tree in C++
In this problem, we are given a binary tree. Our task is to find the largest subtree sum in a tree.
Problem Description: The binary tree consists of positive as well as negative values. And we need to find the subtree that has the maximum sum of nodes.
Let’s take an example to understand the problem,
Output: 13
Explanation:
The sum of left-subtree is 7
The sum of right-subtree is 1
The sum of tree is 13
Solution Approach
To solve the problem, we will do the post-order traversal. Calculate sum of nodes left subtree and right subtree. For current node, check if the sum of nodes of current node is greater than sum of left or right subtree. For each node from leaf to root find the maximum sum.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; struct Node { int key; Node *left, *right; }; Node* newNode(int key) { Node* temp = new Node; temp->key = key; temp->left = temp->right = NULL; return temp; } int calcSumTreeSumRec(Node* root, int& ans) { if (root == NULL) return 0; int currSum = root->key + calcSumTreeSumRec(root->left, ans)+ calcSumTreeSumRec(root->right, ans); ans = max(ans, currSum); return currSum; } int calcMaxSubTreeSum(Node* root) { if (root == NULL) return 0; int ans = -100; calcSumTreeSumRec(root, ans); return ans; } int main() { Node* root = newNode(5); root->left = newNode(-4); root->right = newNode(4); root->left->left = newNode(3); root->left->right = newNode(8); root->right->left = newNode(-5); root->right->right = newNode(2); cout<<"The largest subtree sum is "<<calcMaxSubTreeSum(root); return 0; }
Output
The largest subtree sum is 13
- Related Articles
- Find the largest Complete Subtree in a given Binary Tree in C++
- Find the largest BST subtree in a given Binary Tree - Set 1 in C++
- Find the largest Perfect Subtree in a given Binary Tree in Python
- Find the largest Complete Subtree in a given Binary Tree in Python
- Program to find largest binary search subtree from a given tree in Python
- Largest BST Subtree in C++
- Program to find most frequent subtree sum of a binary tree in Python
- C++ Queries for DFS of a Subtree in a Tree
- Most Frequent Subtree Sum in C++
- Check if a binary tree is subtree of another binary tree in C++
- Even size subtree in n-ary tree in C++
- Find Largest Value in Each Tree Row in C++
- Find the Kth node in the DFS traversal of a given subtree in a Tree in C++
- Program to find a tree by updating values with left and right subtree sum with itself in Python
- Program to find largest sum of any path of a binary tree in Python

Advertisements