
- 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 sum of nodes in Binary tree such that no two are adjacent in C++
In this tutorial, we will be discussing a program to find maximum sum of nodes in Binary tree such that no two are adjacent.
For this we will be provided with a binary tree. Our task is to find the subset having maximum sum such that no two nodes in subset are directly connected.
Example
#include <bits/stdc++.h> using namespace std; //binary tree node structure struct node { int data; struct node *left, *right; }; struct node* newNode(int data) { struct node *temp = new struct node; temp->data = data; temp->left = temp->right = NULL; return temp; } int sumOfGrandChildren(node* node); int getMaxSum(node* node); int getMaxSumUtil(node* node, map<struct node*, int>& mp); int sumOfGrandChildren(node* node, map<struct node*, int>& mp){ int sum = 0; if (node->left) sum += getMaxSumUtil(node->left->left, mp) + getMaxSumUtil(node->left->right, mp); if (node->right) sum += getMaxSumUtil(node->right->left, mp) + getMaxSumUtil(node->right->right, mp); return sum; } //returning maximum sum int getMaxSumUtil(node* node, map<struct node*, int>& mp) { if (node == NULL) return 0; if (mp.find(node) != mp.end()) return mp[node]; int incl = node->data + sumOfGrandChildren(node, mp); int excl = getMaxSumUtil(node->left, mp) + getMaxSumUtil(node->right, mp); mp[node] = max(incl, excl); return mp[node]; } int getMaxSum(node* node) { if (node == NULL) return 0; map<struct node*, int> mp; return getMaxSumUtil(node, mp); } int main() { node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->right->left = newNode(4); root->right->right = newNode(5); root->left->left = newNode(1); cout << getMaxSum(root) << endl; return 0; }
Output
11
- Related Articles
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Maximum sum in circular array such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Program to find maximum sum of non-adjacent nodes of a tree in Python
- Maximum subsequence sum such that no three are consecutive
- Rearrange characters in a string such that no two adjacent are same in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- Maximum subsequence sum such that no three are consecutive in C++ Program
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- Maximum length cycle that can be formed by joining two nodes of a binary tree in C++
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree in C++

Advertisements