You are given an N-ary tree (each node may have 0..k children). Each node contains an integer value (can be 0 or positive).
You want to select a set of nodes such that no selected node is directly connected to another selected node (i.e., you cannot select both a node and any of its children). Return the maximum possible sum of selected node values.
value: int
children: List[Node]
maxNonAdjacentTreeSum(root) -> int
.
root
pointer/reference to the root node (or
null
for empty tree).
If the tree is:
One optimal selection is nodes with values 3 (root) + 3 (grandchild under 2) + 1 = 7, so return 7.
n
up to ~10^5.