Skip to content

Instantly share code, notes, and snippets.

@wohhie
Last active July 9, 2021 01:38
Show Gist options
  • Select an option

  • Save wohhie/0d157c2fc6a7b1837d724231a825d3d8 to your computer and use it in GitHub Desktop.

Select an option

Save wohhie/0d157c2fc6a7b1837d724231a825d3d8 to your computer and use it in GitHub Desktop.
private void iterativePreOrder(Node node) {
if (node == null) return;
// create an empty stack and push root to it
Stack<Node> stack = new Stack<>();
while (node != null || !stack.isEmpty()){
if (node != null){
System.out.print(node.data + " ");
stack.push(node);
node = node.left;
}else{
node = stack.pop();
node = node.right;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment