94. Binary Tree Inorder Traversal - pangeneral/leetcode-notebook GitHub Wiki
This is an iterative solution for tree inorder traversal. The key of the solution lies in:
- Use
current
to denote the node under process; - Use stack to save nodes;
- A node is added to the result list iff it is popped out of the stack
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> resultList = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode current = root;
while (current != null || !stack.isEmpty()) {
while (current != null) {
stack.push(current);
current = current.left;
}
current = stack.pop();
resultList.add(current.val);
current = current.right;
}
return resultList;
}