Skip to content

1932. Merge BSTs to Create Single BST

Description

You are given n BST (binary search tree) root nodes for n separate BSTs stored in an array trees (0-indexed). Each BST in trees has at most 3 nodes, and no two roots have the same value. In one operation, you can:

  • Select two distinct indices i and j such that the value stored at one of the leaves of trees[i] is equal to the root value of trees[j].
  • Replace the leaf node in trees[i] with trees[j].
  • Remove trees[j] from trees.

Return the root of the resulting BST if it is possible to form a valid BST after performing n - 1 operations, ornull if it is impossible to create a valid BST.

A BST (binary search tree) is a binary tree where each node satisfies the following property:

  • Every node in the node's left subtree has a value strictly less than the node's value.
  • Every node in the node's right subtree has a value strictly greater than the node's value.

A leaf is a node that has no children.

 

Example 1:

Input: trees = [[2,1],[3,2,5],[5,4]]
Output: [3,2,5,1,null,4]
Explanation:
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]].

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]].

The resulting tree, shown above, is a valid BST, so return its root.

Example 2:

Input: trees = [[5,3,8],[3,2,6]]
Output: []
Explanation:
Pick i=0 and j=1 and merge trees[1] into trees[0].
Delete trees[1], so trees = [[5,3,8,2,6]].

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.

Example 3:

Input: trees = [[5,4],[3]]
Output: []
Explanation: It is impossible to perform any operations.

 

Constraints:

  • n == trees.length
  • 1 <= n <= 5 * 104
  • The number of nodes in each tree is in the range [1, 3].
  • Each node in the input may have children but no grandchildren.
  • No two roots of trees have the same value.
  • All the trees in the input are valid BSTs.
  • 1 <= TreeNode.val <= 5 * 104.

 

Solutions

Solution: Depth-First Search

  • Time complexity: O(n)
  • Space complexity: O(n)

 

JavaScript

js
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode[]} trees
 * @return {TreeNode}
 */
const canMerge = function (trees) {
  const countMap = new Map();
  const valToNodeMap = new Map();

  const isValidBST = (node, minNode, maxNode) => {
    if (!node) return true;
    if (minNode && node.val <= minNode.val) return false;
    if (maxNode && node.val >= maxNode.val) return false;

    if (!node.left && !node.right && valToNodeMap.has(node.val)) {
      const margeNode = valToNodeMap.get(node.val);

      node.left = margeNode.left;
      node.right = margeNode.right;
      valToNodeMap.delete(node.val);
    }

    return isValidBST(node.left, minNode, node) && isValidBST(node.right, node, maxNode);
  };

  for (const tree of trees) {
    const { val, left, right } = tree;
    const count = countMap.get(val) ?? 0;

    countMap.set(val, count + 1);
    valToNodeMap.set(val, tree);

    if (left) {
      const leftValCount = countMap.get(left.val) ?? 0;

      countMap.set(left.val, leftValCount + 1);
    }

    if (right) {
      const rightValCount = countMap.get(right.val) ?? 0;

      countMap.set(right.val, rightValCount + 1);
    }
  }

  for (const tree of trees) {
    const count = countMap.get(tree.val);

    if (count > 1) continue;
    if (isValidBST(tree, null, null) && valToNodeMap.size <= 1) {
      return tree;
    }

    return null;
  }

  return null;
};

Released under the MIT license