Class Solution
java.lang.Object
g1901_2000.s1932_merge_bsts_to_create_single_bst.Solution
1932 - Merge BSTs to Create Single BST.<p>Hard</p>
<p>You are given <code>n</code> <strong>BST (binary search tree) root nodes</strong> for <code>n</code> separate BSTs stored in an array <code>trees</code> ( <strong>0-indexed</strong> ). Each BST in <code>trees</code> has <strong>at most 3 nodes</strong> , and no two roots have the same value. In one operation, you can:</p>
<ul>
<li>Select two <strong>distinct</strong> indices <code>i</code> and <code>j</code> such that the value stored at one of the <strong>leaves</strong> of <code>trees[i]</code> is equal to the <strong>root value</strong> of <code>trees[j]</code>.</li>
<li>Replace the leaf node in <code>trees[i]</code> with <code>trees[j]</code>.</li>
<li>Remove <code>trees[j]</code> from <code>trees</code>.</li>
</ul>
<p>Return <em>the <strong>root</strong> of the resulting BST if it is possible to form a valid BST after performing</em> <code>n - 1</code> <em>operations, or</em> <code>null</code> <em>if it is impossible to create a valid BST</em>.</p>
<p>A BST (binary search tree) is a binary tree where each node satisfies the following property:</p>
<ul>
<li>Every node in the node’s left subtree has a value <strong>strictly less</strong> than the node’s value.</li>
<li>Every node in the node’s right subtree has a value <strong>strictly greater</strong> than the node’s value.</li>
</ul>
<p>A leaf is a node that has no children.</p>
<p><strong>Example 1:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/06/08/d1.png" alt="" /></p>
<p><strong>Input:</strong> trees = [[2,1],[3,2,5],[5,4]]</p>
<p><strong>Output:</strong> [3,2,5,1,null,4]</p>
<p><strong>Explanation:</strong> In the first operation, pick i=1 and j=0, and merge trees[0] into trees[1]. Delete trees[0], so trees = [[3,2,5,1],[5,4]]. <img src="https://assets.leetcode.com/uploads/2021/06/24/diagram.png" alt="" /> In the second operation, pick i=0 and j=1, and merge trees[1] into trees[0]. Delete trees[1], so trees = [[3,2,5,1,null,4]]. <img src="https://assets.leetcode.com/uploads/2021/06/24/diagram-2.png" alt="" /> The resulting tree, shown above, is a valid BST, so return its root.</p>
<p><strong>Example 2:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/06/08/d2.png" alt="" /></p>
<p><strong>Input:</strong> trees = [[5,3,8],[3,2,6]]</p>
<p><strong>Output:</strong> []</p>
<p><strong>Explanation:</strong> Pick i=0 and j=1 and merge trees[1] into trees[0]. Delete trees[1], so trees = [[5,3,8,2,6]]. <img src="https://assets.leetcode.com/uploads/2021/06/24/diagram-3.png" alt="" /> The resulting tree is shown above. This is the only valid operation that can be performed, but the resulting tree is not a valid BST, so return null.</p>
<p><strong>Example 3:</strong></p>
<p><img src="https://assets.leetcode.com/uploads/2021/06/08/d3.png" alt="" /></p>
<p><strong>Input:</strong> trees = [[5,4],[3]]</p>
<p><strong>Output:</strong> []</p>
<p><strong>Explanation:</strong> It is impossible to perform any operations.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == trees.length</code></li>
<li><code>1 <= n <= 5 * 10<sup>4</sup></code></li>
<li>The number of nodes in each tree is in the range <code>[1, 3]</code>.</li>
<li>Each node in the input may have children but no grandchildren.</li>
<li>No two roots of <code>trees</code> have the same value.</li>
<li>All the trees in the input are <strong>valid BSTs</strong>.</li>
<li><code>1 <= TreeNode.val <= 5 * 10<sup>4</sup></code>.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
canMerge
-