Recover Binary Search Tree🦽

Input: root = [1,3,null,null,2]
Output: [3,1,null,null,2]
Explanation: 3 cannot be a left child of 1 because 3 > 1. Swapping 1 and 3 makes the BST valid.
Input: root = [3,1,4,null,null,2]
Output: [2,1,4,null,null,3]
Explanation: 2 cannot be in the right subtree of 3 because 2 < 3. Swapping 2 and 3 makes the BST valid.
  • The number of nodes in the tree is in the range [2, 1000].
  • -231 <= Node.val <= 231 - 1

Java Solution

Code

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Reuse: a Recipe for Disaster

Azure Databricks from Zero to Hero!

Launching WebServer on AWS with EFS as Storage Using Terraform

Is Blazor the Future or Just Another Walled Garden?

The Mutability Tax

Experimental Solvers: New Capabilities in Fusion 360 Generative Design

What is Temporal.io?

Fun part about C static libraries

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Tehleel Mir 🐢

Tehleel Mir 🐢

More from Medium

Longest Substring Without Repeating Characters📸

Understanding Recursion | Binary Tree

“Delete and Earn” problem using Dynamic Programming

Connecting the dots — Big O and Trees Data Structure