Skip to content

Latest commit

 

History

History
3 lines (2 loc) · 1000 Bytes

Readme.md

File metadata and controls

3 lines (2 loc) · 1000 Bytes

At first, I found a way with easy dfs, when encountering a node, you can decide whether putting the node into the subtree, or starting with this node to form a new subtree. But it would get through many repeating routes, so I used Set to hash Object, it's awesome! See the code here.

Using Set will cost some extra time, here comes the better solution. Think it over, you can start with each of the nodes in the tree, and get downwards to form the subtree, when doing dfs by this way, the only thing you do is putting the node into the subtree(don't need to consider about starting with this passed-by node), so you will not have the repeating routes, and the code will be much quicker. See the complete code here.