Skip to content

Instantly share code, notes, and snippets.

@shharma-vipin
Created August 9, 2019 14:51
Show Gist options
  • Save shharma-vipin/549e0e7bbc4002eeb73ff021c43f8d6b to your computer and use it in GitHub Desktop.
Save shharma-vipin/549e0e7bbc4002eeb73ff021c43f8d6b to your computer and use it in GitHub Desktop.
Inorder Traversal without Recursion
public ArrayList<Integer> inorderTraversal(TreeNode A) {
Stack<TreeNode> stack = new Stack();
TreeNode current = A;
ArrayList<Integer> result = new ArrayList<Integer>();
while(current != null || !stack.isEmpty()){
while(current != null){
stack.push(current);
current = current.left;
}
current = stack.pop();
result.add(current.val);
current = current.right;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment