Yogesh ..’s Post

Day 84/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Recover Binary Search Tree A very interesting problem that tests your understanding of BST inorder properties. Problem idea: Two nodes in a BST are swapped by mistake. Recover the tree without changing its structure. Key idea: Inorder traversal of a BST should be sorted Why? • BST inorder traversal gives increasing order • If two nodes are swapped → order gets violated • Detect these violations to identify the wrong nodes How it works: • Perform inorder traversal • Track previous node (prev) • If prev.val > curr.val → violation found • First violation → mark first = prev • Second violation → mark second = curr • After traversal → swap values of first and second Optimization (used in your code): Use Morris Traversal to achieve O(1) space (no recursion/stack) Time Complexity: O(n) Space Complexity: O(1) (Morris Traversal) Big takeaway: Whenever working with BST, inorder traversal is your strongest tool. 🔥 Also, detecting anomalies in sorted order is a powerful technique. Day 84 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearchTree #MorrisTraversal #InorderTraversal #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories