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

前端 未结 3 479
小蘑菇
小蘑菇 2021-01-14 10:13

By independent nodes, I mean that the returned set can not contain nodes that are in immediate relations, parent and child cannot both be included. I tried to use Google, wi

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-14 10:58

    I would take-and-remove all leaves first while marking their parents as not-to-take, then remove all leaves that are marked until no such leaves are left, then recurse until the tree is empty. I don't have a proof that this always produces the largest possible set, but I believe it should.

提交回复
热议问题