Algorithm-Day37-Binary-Tree-Inorder-Traversal-lc-94
🧩 Problem Description
Given the root
of a binary tree, return its inorder traversal as a list of integers.
Inorder traversal order: left → root → right
💬 Example
1 | Input: root = [1,null,2,3] |
💡 Approach 1: Recursive DFS
The simplest way is to use recursion.
Visit:
- Left subtree
- Current node
- Right subtree
🔢 Java Code (Recursive)
1 | class Solution { |
💡 Approach 2: Iterative with Stack
Use a stack to simulate the recursion.
🔢 Java Code (Iterative)
1 | class Solution { |
⏱ Time and Space Complexity
- Time Complexity: O(n) — each node is visited once
- Space Complexity:
- Recursive: O(h) stack depth (h = height)
- Iterative: O(h) explicit stack
✍️ Summary
- Learn both recursive and iterative versions
- Foundation for tree problems like:
lc-144
: Preorder traversallc-145
: Postorder traversallc-230
: Kth Smallest in BST
Tree traversal mastery is essential for any tree-based problem.