private void compute() { Stack<BSTNode> stack = new Stack<>(); BSTNode<Integer, String> lastVisitedNode = null; while (node != null || !stack.isEmpty()) { if (node != null) { stack.add(node); node = node.left(); } else { BSTNode<Integer, String> peekNode = stack.peek(); if (peekNode.right() != null && lastVisitedNode.element() != peekNode.right().element()) { node = peekNode.right(); } else { if (peekNode.right() == null) System.out.println(stack); lastVisitedNode = stack.pop(); } } } }