Back to all solutions
#94 - Binary Tree Inorder Traversal
Problem Description
Given the root of a binary tree, return the inorder traversal of its nodes' values.
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[]}
*/
var inorderTraversal = function(root) {
const result = [];
const stack = [];
while (root || stack.length) {
if (root) {
stack.push(root);
root = root.left;
} else {
const { val, right } = stack.pop();
result.push(val);
root = right;
}
}
return result;
};