// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
void inorderTraversalHelper(TreeNode* root, vector& result) {
if (root == nullptr) return;
inorderTraversalHelper(root->left, result); // Traverse left subtree
result.push_back(root->val); // Process current node
inorderTraversalHelper(root->right, result); // Traverse right subtree
}
vector inorderTraversal(TreeNode* root) {
vector result;
inorderTraversalHelper(root, result);
return result;
}
};
### Iterative Solution
#include <vector>
#include <stack>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector inorderTraversal(TreeNode* root) {
vector result;
stack stk;
TreeNode* current = root;
while (current != nullptr || !stk.empty()) {
// Reach the leftmost node of the current node
while (current != nullptr) {
stk.push(current);
current = current->left;
}
// Current must be nullptr at this point
current = stk.top();
stk.pop();
result.push_back(current->val); // Process the node
// We have visited the node and its left subtree. Now, it's right subtree's turn
current = current->right;
}
return result;
}
};
## Complexity Analysis
### Recursive Solution
- **Time Complexity**: O(n), where n is the number of nodes in the tree.
- **Space Complexity**: O(h), where h is the height of the tree (due to the call stack).
### Iterative Solution
- **Time Complexity**: O(n), where n is the number of nodes in the tree.
- **Space Complexity**: O(h), where h is the height of the tree (due to the explicit stack).
## Edge Cases
- An empty tree (root is null).
- A tree with only one node.
- A tree where all nodes are on one side (e.g., a linked list).
### Example Edge Cases
1. **Empty Tree**:
- **Input**: `[]`
- **Output**: `[]`
2. **Single Node**:
- **Input**: `[1]`
- **Output**: `[1]`
3. **Linked List (All Right Nodes)**:
- **Input**: `[1, null, 2, null, 3]`
- **Output**: `[1, 2, 3]`
## Testing
### Comprehensive Testing
- Test with a variety of tree structures.
- Include edge cases and large trees.
- Use testing frameworks like Google Test for automated testing.
### Example Test Cases
1. Balanced tree.
2. Unbalanced tree.
3. Trees with varying depths.
## Thinking and Problem-Solving Tips
- Break down the problem into smaller parts.
- Visualize the tree and the traversal process.
- Practice with different tree structures to understand the traversal order.
## Conclusion
Understanding and implementing binary tree traversals is fundamental in computer science. The iterative approach to inorder traversal is efficient and avoids the pitfalls of recursion. Practice and familiarity with different tree structures will enhance problem-solving skills.
## Additional Resources
- [Binary Tree Traversal Techniques](https://en.wikipedia.org/wiki/Tree_traversal)
- [LeetCode Problems on Tree Traversals](https://leetcode.com/tag/tree/)
- [GeeksforGeeks Tree Traversal](https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/)
By mastering these concepts, you will be well-equipped to handle a wide range of tree-related problems in coding interviews and real-world applications.
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