java.lang.Object
g0001_0100.s0099_recover_binary_search_tree.Solution

public class Solution extends Object
99 - Recover Binary Search Tree.<p>Medium</p> <p>You are given the <code>root</code> of a binary search tree (BST), where the values of <strong>exactly</strong> two nodes of the tree were swapped by mistake. <em>Recover the tree without changing its structure</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/10/28/recover1.jpg" alt="" /></p> <p><strong>Input:</strong> root = [1,3,null,null,2]</p> <p><strong>Output:</strong> [3,1,null,null,2]</p> <p><strong>Explanation:</strong> 3 cannot be a left child of 1 because 3 > 1. Swapping 1 and 3 makes the BST valid.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/10/28/recover2.jpg" alt="" /></p> <p><strong>Input:</strong> root = [3,1,4,null,null,2]</p> <p><strong>Output:</strong> [2,1,4,null,null,3]</p> <p><strong>Explanation:</strong> 2 cannot be in the right subtree of 3 because 2 < 3. Swapping 2 and 3 makes the BST valid.</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[2, 1000]</code>.</li> <li><code>-2<sup>31</sup> <= Node.val <= 2<sup>31</sup> - 1</code></li> </ul> <p><strong>Follow up:</strong> A solution using <code>O(n)</code> space is pretty straight-forward. Could you devise a constant <code>O(1)</code> space solution?</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • recoverTree

      public void recoverTree(TreeNode root)