Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public Node findRoot(List<Node> tree) {
- Set<Node> childNodes = new HashSet<>();
- for(Node node : tree) {
- for (Node childNode : node.children) {
- childNodes.add(childNode);
- }
- }
- for(Node node : tree) {
- if (!childNodes.contains(node)) {
- return node;
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement