Java algorithm for finding the largest set of independent nodes in a binary tree

By independent nodes, I mean that the returned set cannot contain nodes that are in a direct relationship, the parent and child cannot be included. I tried to use Google without success. I don’t think I have the right search words.

Link, any help would be greatly appreciated. Just started it now.

I need to return the actual set of independent nodes, not just the amount.

+3
source share
2 answers

You can compute this recursive function using dynamic programming (memoization):

MaxSet(node) = 1 if "node" is a leaf
MaxSet(node) = Max(1 + Sum{ i=0..3: MaxSet(node.Grandchildren[i]) },  
                       Sum{ i=0..1: MaxSet(node.Children[i])      })

, node . , , . , .

, , "" node. LCS.

O (n). , .

+6

, , , , , , . , , , .

0

Source: https://habr.com/ru/post/1720095/


All Articles