# Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes’ values.

``````var inorderTraversal = function(root) {
//recursive approach
let result=[];
if (root===null) {
return result;
}
//go left node
if (root.left) {
result=result.concat(inorderTraversal(root.left));
}
result=result.concat([root.val]);
if (root.right) {
result=result.concat(inorderTraversal(root.right));
}
//console.log("result ",result);
return result;
};``````

Approaches
Inorder traversal of a Binary Tree is a depth-first search (DFS) traversal, where the left bottom most element is the starting point and it goes from left to right at the lowest depths first. There are two approaches to doing this, one recursive and one iterative. We’ll look at the recursive approach here first.

Inorder traversal strategy for recursive is:

• Recurse on left node
• Action on current node
• Recurse on right node

Code Breakdown
Let’s go over the solution block by block and explain as we go.

``````    let result=[];
if (root===null) {
return result;
}``````

We initialize the result variable to be an empty array. This will make sense later once you see what we do with the return value from the recursive calls.
The stopping condition for this recursion is when root is null. This means we’re passed a child node that doesn’t exist. We return an empty array in this case.

``````    //go left node
if (root.left) {
result=result.concat(inorderTraversal(root.left));
}