How to use the Binary Tree Traversal Generator
- 1. Choose the input typeLevel order with nulls (LeetCode style) or numbers inserted into a BST.
- 2. Type the valuesSeparate them with commas or spaces. Use null for a missing child.
- 3. Read the traversalsSee the tree, the four orders, height, node count and leaves.
The three depth-first orders
All three depth-first traversals visit the left subtree before the right one. They differ only in when the root is written down:
- Preorder: Root, Left, Right. Useful to copy or serialise a tree.
- Inorder: Left, Root, Right. For a binary search tree this gives the values in sorted order.
- Postorder: Left, Right, Root. Used to delete a tree or evaluate an expression tree, since children are handled before their parent.
Level order is breadth-first: it reads the tree row by row from the top using a queue.
Worked example
Enter 1, 2, 3, 4, 5, null, 7 in level order. Node 1 is the root, 2 and 3 are its children, 4 and 5 hang under 2, and under 3 the left child is missing (null) and the right child is 7. The tool gives:
- Preorder: 1 2 4 5 3 7
- Inorder: 4 2 5 1 3 7
- Postorder: 4 5 2 7 3 1
- Level order: 1 2 3 4 5 7
It also reports 6 nodes, height 3 and 3 leaves (4, 5 and 7). Switch to BST mode and enter 50 30 70 20 40 60 80: the inorder result is 20 30 40 50 60 70 80, sorted as expected, which is a quick way to check your own BST code. Every traversal visits each node once, so all of them run in O(n) time.
Frequently asked questions
How is the level-order input read?
Values fill the tree row by row, left to right, the same way LeetCode builds trees. A null means that child is empty, and nulls do not get children of their own.
Can a tree be rebuilt from its traversals?
Yes, from inorder plus either preorder or postorder, as long as the values are unique. Preorder and postorder alone are not enough for a general binary tree.
What does height mean here?
The number of levels. A single node has height 1 in this tool. Some textbooks count edges instead and would call that height 0.
Is this traversal tool free?
Yes, it is free. Your first result asks for your name and WhatsApp number once, and after that every Uncodemy tool opens without the form.