// Definition for a binary tree node.
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
inorderHelper(root, result);
return result;
}
private void inorderHelper(TreeNode node, List<Integer> result) {
if (node == null) {
return;
}
// Traverse left subtree
inorderHelper(node.left, result);
// Visit node
result.add(node.val);
// Traverse right subtree
inorderHelper(node.right, result);
}
}
### Iterative Solution
public class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode current = root;
while (current != null || !stack.isEmpty()) {
// Reach the leftmost node of the current node
while (current != null) {
stack.push(current);
current = current.left;
}
// Current must be null at this point
current = stack.pop();
result.add(current.val);
// Visit the right subtree
current = current.right;
}
return result;
}
}
## Complexity Analysis
### Recursive Solution
- **Time Complexity**: O(n), where n is the number of nodes. Each node is visited once.
- **Space Complexity**: O(h), where h is the height of the tree. This is due to the recursion stack.
### Iterative Solution
- **Time Complexity**: O(n), where n is the number of nodes. Each node is visited once.
- **Space Complexity**: O(n), in the worst case, the stack will store all nodes in the tree.
## Edge Cases
- **Empty Tree**: The output should be an empty list.
- **Single Node Tree**: The output should be a list with one element.
- **Skewed Tree**: The tree is skewed to the left or right, testing the stack's handling of such cases.
## Testing
To test the solution comprehensively:
- Test with an empty tree.
- Test with a single node.
- Test with a balanced tree.
- Test with a skewed tree (left and right).
## Thinking and Problem-Solving Tips
- Understand the traversal order and practice with different tree structures.
- Use diagrams to visualize the stack operations in the iterative approach.
- Practice converting recursive solutions to iterative ones using a stack.
## Conclusion
Inorder traversal is a fundamental tree traversal technique with various applications. Understanding both recursive and iterative approaches is crucial for solving related problems efficiently.
## Additional Resources
- [Binary Tree Traversal](https://en.wikipedia.org/wiki/Tree_traversal)
- [LeetCode Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)
- [GeeksforGeeks Inorder Traversal](https://www.geeksforgeeks.org/inorder-tree-traversal-without-recursion/)
By mastering these techniques, you can tackle a wide range of tree-related problems with confidence. Happy coding!
Our interactive tutorials and AI-assisted learning will help you master problem-solving skills and teach you the algorithms to know for coding interviews.
Start Coding for FREE