返回介绍

solution / 0700-0799 / 0776.Split BST / README_EN

发布于 2024-06-17 01:03:34 字数 5513 浏览 0 评论 0 收藏 0

776. Split BST

中文文档

Description

Given the root of a binary search tree (BST) and an integer target, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the target value. It Is not necessarily the case that the tree contains a node with the value target.

Additionally, most of the structure of the original tree should remain. Formally, for any child c with parent p in the original tree, if they are both in the same subtree after the split, then node c should still have the parent p.

Return _an array of the two roots of the two subtrees_.

 

Example 1:

Input: root = [4,2,6,1,3,5,7], target = 2
Output: [[2,1],[4,3,6,null,null,5,7]]

Example 2:

Input: root = [1], target = 1
Output: [[1],[]]

 

Constraints:

  • The number of nodes in the tree is in the range [1, 50].
  • 0 <= Node.val, target <= 1000

Solutions

Solution 1

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, val=0, left=None, right=None):
#     self.val = val
#     self.left = left
#     self.right = right
class Solution:
  def splitBST(
    self, root: Optional[TreeNode], target: int
  ) -> List[Optional[TreeNode]]:
    def dfs(root):
      if root is None:
        return [None, None]
      if root.val <= target:
        l, r = dfs(root.right)
        root.right = l
        return [root, r]
      else:
        l, r = dfs(root.left)
        root.left = r
        return [l, root]

    return dfs(root)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode() {}
 *   TreeNode(int val) { this.val = val; }
 *   TreeNode(int val, TreeNode left, TreeNode right) {
 *     this.val = val;
 *     this.left = left;
 *     this.right = right;
 *   }
 * }
 */
class Solution {
  private int t;

  public TreeNode[] splitBST(TreeNode root, int target) {
    t = target;
    return dfs(root);
  }

  private TreeNode[] dfs(TreeNode root) {
    if (root == null) {
      return new TreeNode[] {null, null};
    }
    if (root.val <= t) {
      TreeNode[] ans = dfs(root.right);
      root.right = ans[0];
      ans[0] = root;
      return ans;
    } else {
      TreeNode[] ans = dfs(root.left);
      root.left = ans[1];
      ans[1] = root;
      return ans;
    }
  }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *   TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *   TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  int t;

  vector<TreeNode*> splitBST(TreeNode* root, int target) {
    t = target;
    return dfs(root);
  }

  vector<TreeNode*> dfs(TreeNode* root) {
    if (!root) return {nullptr, nullptr};
    if (root->val <= t) {
      auto ans = dfs(root->right);
      root->right = ans[0];
      ans[0] = root;
      return ans;
    } else {
      auto ans = dfs(root->left);
      root->left = ans[1];
      ans[1] = root;
      return ans;
    }
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func splitBST(root *TreeNode, target int) []*TreeNode {
  if root == nil {
    return []*TreeNode{nil, nil}
  }
  if root.Val <= target {
    ans := splitBST(root.Right, target)
    root.Right = ans[0]
    ans[0] = root
    return ans
  } else {
    ans := splitBST(root.Left, target)
    root.Left = ans[1]
    ans[1] = root
    return ans
  }
}
/**
 * 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} root
 * @param {number} target
 * @return {TreeNode[]}
 */
var splitBST = function (root, target) {
  let ans = [null, null];
  if (!root) {
    return ans;
  }
  if (root.val <= target) {
    ans = splitBST(root.right, target);
    root.right = ans[0];
    ans[0] = root;
  } else {
    ans = splitBST(root.left, target);
    root.left = ans[1];
    ans[1] = root;
  }
  return ans;
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文