Skip to content

Instantly share code, notes, and snippets.

@adyatlov
Created May 12, 2014 13:22
Show Gist options
  • Save adyatlov/643a14d1c9f6f7b4ad71 to your computer and use it in GitHub Desktop.
Save adyatlov/643a14d1c9f6f7b4ad71 to your computer and use it in GitHub Desktop.
TreeItem findStack2(TreeItem root, Object obj) {
maxStackSize = 0;
Deque<Iterator<TreeItem>> stack = new ArrayDeque<Iterator<TreeItem>>();
stack.push(Collections.singletonList(root).iterator());
while (!stack.isEmpty()) {
Iterator<TreeItem> it = stack.peek();
if (it.hasNext()) {
TreeItem item = it.next();
if (Objects.equals(item.getData(), obj)) {
return item;
}
stack.push(item.getChildren().iterator());
maxStackSize = Math.max(maxStackSize, stack.size());
} else {
stack.pop();
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment