Problem Description
Given the root of a binary tree, return the inorder traversal of its nodes' values.
Examples
Example 1: Input: root = [1,null,2,3] Output: [1,3,2] Example 2: Input: root = [] Output: [] Example 3: Input: root = [1] Output: [1]
Python Solution
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
# Recursive solution
def inorderTraversal(root: Optional[TreeNode]) -> List[int]:
def inorder(node: Optional[TreeNode]):
if not node:
return
inorder(node.left)
result.append(node.val)
inorder(node.right)
result = []
inorder(root)
return result
# Iterative solution
def inorderTraversal(root: Optional[TreeNode]) -> List[int]:
result = []
stack = []
curr = root
while curr or stack:
# Reach the leftmost node
while curr:
stack.append(curr)
curr = curr.left
curr = stack.pop()
result.append(curr.val)
curr = curr.right
return result
Java Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
// Recursive solution
private List result;
public List inorderTraversal(TreeNode root) {
result = new ArrayList<>();
inorder(root);
return result;
}
private void inorder(TreeNode node) {
if (node == null) {
return;
}
inorder(node.left);
result.add(node.val);
inorder(node.right);
}
// Iterative solution
public List inorderTraversalIterative(TreeNode root) {
List result = new ArrayList<>();
Stack stack = new Stack<>();
TreeNode curr = root;
while (curr != null || !stack.isEmpty()) {
// Reach the leftmost node
while (curr != null) {
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
result.add(curr.val);
curr = curr.right;
}
return result;
}
}
C++ Solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
private:
vector result;
void inorder(TreeNode* node) {
if (!node) {
return;
}
inorder(node->left);
result.push_back(node->val);
inorder(node->right);
}
public:
// Recursive solution
vector inorderTraversal(TreeNode* root) {
result.clear();
inorder(root);
return result;
}
// Iterative solution
vector inorderTraversalIterative(TreeNode* root) {
vector result;
stack stack;
TreeNode* curr = root;
while (curr || !stack.empty()) {
// Reach the leftmost node
while (curr) {
stack.push(curr);
curr = curr->left;
}
curr = stack.top();
stack.pop();
result.push_back(curr->val);
curr = curr->right;
}
return result;
}
};
JavaScript Solution
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
// Recursive solution
var inorderTraversal = function(root) {
const result = [];
const inorder = (node) => {
if (!node) {
return;
}
inorder(node.left);
result.push(node.val);
inorder(node.right);
};
inorder(root);
return result;
};
// Iterative solution
var inorderTraversalIterative = function(root) {
const result = [];
const stack = [];
let curr = root;
while (curr || stack.length) {
// Reach the leftmost node
while (curr) {
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
result.push(curr.val);
curr = curr.right;
}
return result;
};
C# Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private IList result;
// Recursive solution
public IList InorderTraversal(TreeNode root) {
result = new List();
Inorder(root);
return result;
}
private void Inorder(TreeNode node) {
if (node == null) {
return;
}
Inorder(node.left);
result.Add(node.val);
Inorder(node.right);
}
// Iterative solution
public IList InorderTraversalIterative(TreeNode root) {
IList result = new List();
Stack stack = new Stack();
TreeNode curr = root;
while (curr != null || stack.Count > 0) {
// Reach the leftmost node
while (curr != null) {
stack.Push(curr);
curr = curr.left;
}
curr = stack.Pop();
result.Add(curr.val);
curr = curr.right;
}
return result;
}
}
Complexity Analysis
- 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
Solution Explanation
This solution provides both recursive and iterative approaches:
- Recursive approach:
- Visit left subtree
- Process current node
- Visit right subtree
- Iterative approach:
- Use stack to track nodes
- Reach leftmost node first
- Process node and go right
Key points:
- Left-Root-Right order
- Stack-based iteration
- Handles empty tree
- Both solutions optimal