- 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

# Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming 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 using Dynamic Programming.

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 the subset are directly connected using Dynamic Programming.

## Example

#include <bits/stdc++.h> using namespace std; //finding diameter using dynamic programming void dfs(int node, int parent, int dp1[], int dp2[], list<int>* adj, int tree[]){ int sum1 = 0, sum2 = 0; for (auto i = adj[node].begin(); i != adj[node].end(); ++i) { if (*i == parent) continue; dfs(*i, node, dp1, dp2, adj, tree); sum1 += dp2[*i]; sum2 += max(dp1[*i], dp2[*i]); } dp1[node] = tree[node] + sum1; dp2[node] = sum2; } int main() { int n = 5; list<int>* adj = new list<int>[n + 1]; adj[1].push_back(2); adj[2].push_back(1); adj[1].push_back(3); adj[3].push_back(1); adj[2].push_back(4); adj[4].push_back(2); adj[2].push_back(5); adj[5].push_back(2); int tree[n + 1]; tree[1] = 10; tree[2] = 5; tree[3] = 11; tree[4] = 6; tree[5] = 8; int dp1[n + 1], dp2[n + 1]; memset(dp1, 0, sizeof dp1); memset(dp2, 0, sizeof dp2); dfs(1, 1, dp1, dp2, adj, tree); cout << "Maximum sum: " << max(dp1[1], dp2[1]) << endl; return 0; }

## Output

Maximum sum: 25

- Related Articles
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- 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++
- Rearrange characters in a string such that no two adjacent are same 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++
- Print path between any two nodes in a Binary Tree in C++ Programming.
- Program to find maximum sum of non-adjacent nodes of a tree in Python
- Maximum subsequence sum such that no three are consecutive
- 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++

Advertisements