Question

In Java using a Dynamic Programming technique: Maximum sum of nodes in a binary tree such...

In Java using a Dynamic Programming technique: Maximum sum of nodes in a binary tree such that no two nodes are adjacent. The dynamic programming part is the part I am having trouble with

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.ArrayList;

public class Main {

public void findMaximumSum(int node, int parent, int dp1[], int dp2[], ArrayList<ArrayList<Integer>> adj, int tree[]) {

int sum1 = 0, sum2 = 0;

for (ArrayList<Integer> adjNodes : adj) {

for (Integer n : adjNodes) {

if (n == parent)

continue;

findMaximumSum(n, node, dp1, dp2, adj, tree);

sum1 += dp2[n];

sum2 += Math.max(dp1[n], dp2[n]);

}

}

dp1[node] = tree[node] + sum1;

dp2[node] = sum2;

}

}

Add a comment
Know the answer?
Add Answer to:
In Java using a Dynamic Programming technique: Maximum sum of nodes in a binary tree such...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT