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.