Data Structures & Algorithms
Tree Traversal Algorithms Practice
Master tree traversal algorithms with our interactive guide. Get expert-verified solutions, practice problems, and AI-powered feedback to ace your coding interviews.
Binary Tree Inorder Traversal
Given the root of a binary tree, return the inorder traversal of its nodes' values.
class Solution {
inorderTraversal(root) {
const result = [];
const stack = [];
let current = root;
while (current || stack.length) { while (current) { stack.push(current); current = current.left; } current = stack.pop(); result.push(current.val); current = current.right; }
return result; } }
while (current || stack.length) { while (current) { stack.push(current); current = current.left; } current = stack.pop(); result.push(current.val); current = current.right; }
return result; } }
This solution uses an iterative approach with a stack to perform an inorder traversal. We traverse as far left as possible, pushing nodes onto the stack. When we can't go left anymore, we pop a node, visit it (add its value to the result), and then move to its right child. This process is repeated until both the current node is null and the stack is empty.
Related Algorithm Guides
Explore more algorithm interview guides powered by AI coaching
Bst Vs Hash Table Interview Questions
AI-powered interview preparation guide
Avl Tree Interview Questions
AI-powered interview preparation guide
Graduate Hr Assistant Competency Interview Answers
AI-powered interview preparation guide
Intelligent Interview Question Analysis
AI-powered interview preparation guide