Obtain forest out of tree with even number of nodes

后端 未结 8 502
别跟我提以往
别跟我提以往 2021-02-03 20:31

I\'m stuck on a code challenge, and I want a hint.

PROBLEM: You are given a tree data structure (without cycles) and are asked to remo

8条回答
  •  梦如初夏
    2021-02-03 21:12

    Here's the approach that I used to successfully pass all the test cases.

    1. Mark vertex 1 as the root
    2. Starting at the current root vertex, consider each child. If the sum total of the child and all of its children are even, then you can cut that edge
    3. Descend to the next vertex (child of root vertex) and let that be the new root vertex. Repeat step 2 until you have traversed all of the nodes (depth first search).

提交回复
热议问题