Removal of subtrees in tree on condition optimally

i have mini program that removes subtrees of a tree based on following condition: If a subtree is consisting only of nodes with “rank” 1 remove it. I am using recursive DFS approach. Trees or subtrees does not have any further special properties. I want to ask about your opinions. Is it posibble to make this more elegant/more optimal?. Thanks.

public class Main {      public static void main(String[] args) {         List<Node> tree1 = Arrays.asList(             new Node(1, null, 1),             new Node(2, 1, 1),             new Node(3, 1, 1),             new Node(4, 2, 0),             new Node(5, 3, 1),             new Node(6, 5, 0),             new Node(7, 5, 1),             new Node(8, 7, 0),             new Node(9, 5, 1),             new Node(10, 3, 1)         );          List<Node> newTree = new ArrayList<>();         traverse(tree1.get(0), tree1, newTree);         if(tree1.get(0).rank != 1 || tree1.get(0).getNumberOfValidSubtrees() != 0) {             newTree.add(tree1.get(0));         }          System.out.println(tree1);         newTree.sort(Comparator.comparing(Node::getId));         System.out.println(newTree);     }      private static void traverse(Node parent, List<Node> allNodes, List<Node> newTree){         List<Node> children = allNodes.stream().filter(e -> e.parentId != null && e.parentId == parent.id).collect(Collectors.toList());          for(Node child : children) {             traverse(child, allNodes, newTree);              if(child.rank != 1 || child.getNumberOfValidSubtrees() != 0) {                 parent.incrementValidSubtree();                 newTree.add(child);             }         }     } } 

Node class

class Node{     int id;     Integer parentId;      int rank;     private int numberOfValidSubtrees;      Node(int id, Integer parentId, int rank){         this.id = id;         this.parentId = parentId;         this.rank = rank;         this.numberOfValidSubtrees = 0;     }      public int getNumberOfValidSubtrees(){         return this.numberOfValidSubtrees;     }      public void incrementValidSubtree() {         this.numberOfValidSubtrees++;     }      public int getId() {         return id;     }      public String toString() {         return String.valueOf(id);     } }