π A Weird Glitch in BST Tree Building!

Hey folks! I'm Subhayan Mukherjee, a CSE student from NIT Durgapur β and today I stumbled upon something super quirky while working on Binary Search Trees (BSTs).
You probably learned in DSA class that:
"If you have both inorder and preorder/postorder traversals of a binary tree, you can uniquely reconstruct the tree."
Well... hold up. That's not always true. At least, not when the tree is a BST!
π§ What I Noticed (And It Blew My Mind)
Imagine this:
Postorder of a BST:
[5, 17, 19, 18, 16, 70, 60, 20]
Inorder (which for a BST is just sorted):
[5, 16, 17, 18, 19, 20, 60, 70]
You'd think there's only one BST that could result in this, right? Nope.
You can actually build two different BSTs from this combo β both are valid and follow BST rules, but their structures differ.
π Visual Time!
Let me show you the two BSTs (you can spot the difference):
π BST 1: Classic Build
π BST 2: A Twist in Shape
Cool, right? Same postorder, same inorder, different trees.
π So... What Does This Mean?
The classic DSA theorem works great for general binary trees, but when we restrict the structure to BSTs (with that left < root < right rule), ambiguity creeps in.
Why? Because BSTs donβt say how deep a node should be β just that it's in the right range. That leaves room for multiple valid arrangements.
π€― The Takeaway
Even if you know:
The inorder (sorted list)
The postorder (or preorder)
You still might not know the exact shape of the BST.
So next time you're solving a reconstruction problem, ask yourself:
Am I building any binary tree, or a BST?
π About Me
Hey, Iβm Subhayan Mukherjee β a curious soul who loves exploring odd cases in computer science. When I'm not debugging something, youβll probably find me making music or asking weird algorithmic questions.
If you liked this post, let's connect:
LinkedIn:-https://www.linkedin.com/in/subhayan-mukherjee-b24b5435a/
Medium:-https://medium.com/@edu.subhayan
π Drop your thoughts!
Have you ever stumbled upon a similar quirky case in algorithms or data structures? Share it in the comments!
Subscribe to my newsletter
Read articles from Subhayan Mukherjee directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
