Finding size of max independent set in binary tree - why faulty “solution” doesn't work?

后端 未结 2 1554
小鲜肉
小鲜肉 2021-01-23 08:54

Here is a link to a similar question with a good answer: Java Algorithm for finding the largest set of independent nodes in a binary tree.

I came up with a different ans

2条回答
  •  醉话见心
    2021-01-23 09:45

    Your algorithm doesn't work because the set of nodes it returns will be either all from odd levels, or all from even levels. But the optimal solution can have nodes from both.

    For example, consider a tree where all weights are 0 except for two nodes which have weight 1. One of these nodes is at level 1 and the other is at level 4. The optimal solution will contain both these nodes and have weight 2. But your algorithm will only give one of these nodes and have weight 1.

提交回复
热议问题