-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maximum sum with out adjacent nodes
57 lines (50 loc) · 1.31 KB
/
Maximum sum with out adjacent nodes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
/************************************************************
Following is the TreeNode class structure
template <typename T>
class TreeNode {
public:
T data;
TreeNode<T> *left;
TreeNode<T> *right;
TreeNode(T data) {
this->data = data;
left = NULL;
right = NULL;
}
};
************************************************************/
#include <unordered_map>
int helper(TreeNode <int> * root , unordered_map < TreeNode<int> * , int > &dp )
{
if(root == NULL)
{
return 0 ;
}
if(dp.count(root) > 0)
{
return dp[root] ;
}
int rr = 0 , rl = 0 , lr = 0 , ll = 0;
if(root->left != NULL)
{
ll = helper(root->left->left ,dp) ;
lr = helper(root->left->right ,dp) ;
}
if(root->right != NULL)
{
rl = helper(root->right->left , dp) ;
rr = helper(root->right->right , dp) ;
}
int include = ll + lr + rr + rl + root->data ;
int l = helper(root->left,dp) ;
int r = helper(root->right,dp) ;
return dp[root] = max(l + r , include) ;
}
int maximumSumOfNodes(TreeNode<int> *root)
{
// Write your code here
//include
unordered_map < TreeNode<int> * , int > dp ;
return helper(root , dp) ;
}