Created
August 9, 2019 14:51
-
-
Save shharma-vipin/549e0e7bbc4002eeb73ff021c43f8d6b to your computer and use it in GitHub Desktop.
Inorder Traversal without Recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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