
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum parent children sum in Binary tree in C++
In this tutorial, we will be discussing a program to find maximum parent children sum in Binary tree
For this we will be provided with a binary tree. Our task is to add up the parent node with its children nodes and finally find the maximum of all of that and print it out.
Example
#include <iostream> using namespace std; struct Node { int data; struct Node *left, *right; }; //inserting nodes struct Node* newNode(int n) { struct Node* root = new Node(); root->data = n; root->left = root->right = NULL; return root; } int maxSum(struct Node* root) { if (root == NULL) return 0; int res = maxSum(root->left); if (root->left != NULL && root->right != NULL) { int sum = root->data + root->left->data + root->right->data; res = max(res, sum); } return max(res, maxSum(root->right)); } int main() { struct Node* root = newNode(15); root->left = newNode(16); root->left->left = newNode(8); root->left->left->left = newNode(55); root->left->right = newNode(67); root->left->right->left = newNode(44); root->right = newNode(17); root->right->left = newNode(7); root->right->left->right = newNode(11); root->right->right = newNode(41); cout << maxSum(root); return 0; }
Output
91
- Related Articles
- Binary Tree Maximum Path Sum in Python
- Check for Children Sum Property in a Binary Tree in C++
- Maximum spiral sum in Binary Tree in C++
- Maximum Sum BST in Binary Tree in C++
- Find maximum vertical sum in binary tree in C++
- Maximum Path Sum in a Binary Tree in C++
- Find maximum level sum in Binary Tree in C++
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property in C++
- Maximum Level Sum of a Binary Tree in C++
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++
- Maximum Binary Tree in C++
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++ program
- Binary Search Tree insert with Parent Pointer in C++
- Maximum Binary Tree II in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++

Advertisements