Binary Trees - Inorder Traversal
Binary Trees
JavaScript
An inorder traversal technique follows the Left -> Root -> Right order.
Inorder traversal is a method of visiting all the nodes in a binary tree in a specific order. In this traversal, the left subtree is processed first, then the root node is visited, and finally, the traversal continues through the right subtree.
Algorithm:
- Treverse the left subtree (recursively).
- Visit the current root node
- Treverse the right subtree (recursively).
So the tree is traversed along the left subtree until the end node on the left subtree is reached. Then this end node is processed. Finally the same process is performed on the right subtree starting from this end node.
Characteristics of Inorder Traversal
- If the binary tree is a binary search tree, the inorder traversal will visit the nodes in ascending order.
- Time Complexity: Inorder traversal of a tree with 𝑛 nodes takes 𝑂(𝑛) time, as it visits each node exactly once.
Compare with other traversal algorithms
Preorder Traversal: Root -> Left -> Right
Postorder Traversal: Left -> Right -> Root
Solution & Visualize:
The animation/visualization below demonstrates how the Inorder traversal works.
Result
Code
Input:
The tree nodes should only contain the numbers or `null` followed by comma
solution.js
binary-tree.js