Advertisement
Guest User

Grokking 231

a guest
Aug 25th, 2022
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.43 KB | None | 0 0
  1. class Solution {
  2.     public Node findRoot(List<Node> tree) {
  3.         Set<Node> childNodes = new HashSet<>();
  4.  
  5.         for(Node node : tree) {
  6.             for (Node childNode : node.children) {
  7.                 childNodes.add(childNode);
  8.             }
  9.         }
  10.  
  11.         for(Node node : tree) {
  12.             if (!childNodes.contains(node)) {
  13.                 return node;
  14.             }
  15.         }
  16.  
  17.         return null;
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement