[LeetCode] Split BST 分割二叉搜索树

Given a Binary Search Tree (BST) with root node root, and a target value V, 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‘s not necessarily the case that the tree contains a node with value V.

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.

You should output the root TreeNode of both subtrees after splitting, in any order.

Example 1:

Input: root = [4,2,6,1,3,5,7], V = 2
Output: [[2,1],[4,3,6,null,null,5,7]]
Explanation:
Note that root, output[0], and output[1] are TreeNode objects, not arrays.

The given tree [4,2,6,1,3,5,7] is represented by the following diagram:

          4
        /         2      6
     / \    /     1   3  5   7

while the diagrams for the outputs are:

          4
        /         3      6      and    2
            / \           /
           5   7         1

Note:

  1. The size of the BST will not exceed 50.
  2. The BST is always valid and each node‘s value is different.

s

原文地址:https://www.cnblogs.com/grandyang/p/8993143.html

时间: 2024-07-31 12:17:33

[LeetCode] Split BST 分割二叉搜索树的相关文章

LeetCode.938-范围内求二叉搜索树节点值之和(Range Sum of BST)

这是悦乐书的第359次更新,第386篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第221题(顺位题号是938).给定二叉搜索树的根节点,返回节点值在[L,R]之间的所有节点的值的总和.二叉搜索树的节点值唯一.例如: 输入:root = [10,5,15,3,7,null,18],L = 7,R = 15 输出:32 输入:root = [10,5,15,3,7,13,18,1,null,6],L = 6,R = 10 输出:23 注意: 树中的节点数最多为1000

BST(二叉搜索树) Java 实现解析

1.二叉搜索树的定义:一颗树的所有左子树都比根小,所有右子树都比根大,成为二叉搜索树. 2.该BST树实现了9个重要方法,分别是关键字查找,插入,删除,删除节点后续节点查找,前序遍历,中序遍历,后序遍历,获取最大节点,获取最小节点. 3.以下是Java的代码实现: //定义Node类,拥有元素值,节点名称,左孩子节点,右孩子节点,4个成员变量.class Node { int element; String name; Node leftChild; Node rightChild; publi

Leetcode 96. 不同的二叉搜索树

题目链接 https://leetcode.com/problems/unique-binary-search-trees/description/ 题目描述 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种不同结构的二叉搜索树: 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 题解 首先定义一个函数G[n]:表示1...n构成的二叉搜索树的个数.1..

LeetCode动画 | 1038. 从二叉搜索树到更大和树

今天分享一个LeetCode题,题号是1038,标题是:从二分搜索树到更大和数. 题目描述 给出二叉搜索树的根节点,该二叉树的节点值各不相同,修改二叉树,使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和. 提醒一下,二叉搜索树满足下列约束条件: 1)节点的左子树仅包含键小于节点键的节点. 2)节点的右子树仅包含键大于节点键的节点. 3)左右子树也必须是二叉搜索树. 示例: 输入:[4, 1, 6, 0, 2, 5, 7, null, null, null, 3, nu

LeetCode 96——不同的二叉搜索树

1. 题目 2. 解答 以 \(1, 2, \cdots, n\) 构建二叉搜索树,其中,任意数字都可以作为根节点来构建二叉搜索树.当我们将某一个数字作为根节点后,其左边数据将构建为左子树,右边数据将构建为右子树.因此,这是一个递归问题. 若以第 \(i\) 个数据为根节点,其左边数据有 \(i-1\) 个,左子树可能情况为 left_num,右边数据有 \(n-i\) 个,右子树可能情况为 right_num,因此以当前数据为根节点可以构建出 left_num * right_num 个二叉搜

Leetcode 95.不同的二叉搜索树II

给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树. 示例: 输入: 3 输出: [   [1,null,3,2],   [3,2,null,1],   [3,1,null,null,2],   [2,1,3],   [1,null,2,null,3] ] 解释: 以上的输出对应以下 5 种不同结构的二叉搜索树: 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 1 /** 2 * Definition for a binary tr

[CareerCup] 4.6 Find Next Node in a BST 寻找二叉搜索树中下一个节点

4.6 Write an algorithm to find the'next'node (i.e., in-order successor) of a given node in a binary search tree. You may assume that each node has a link to its parent.

二叉搜索树 并查集

BST:二叉搜索树 二叉搜索树的特点:左孩子<根节点<右孩子 二叉搜索树可以有效的管理数的集合 (1)查找 查找数值,大于根节点向右查找,小于根节点向做查找,递归直到找到或者找不到为止. (2)插入 类似于查找,找到相应的位置之后,将那个位置插入新的节点即可. (3)删除 删除就稍微的复杂一点: 需要删除的点没有左孩子,直接将右孩子提上去 需要删除的点的左孩子没有右孩子,直接将左孩子提上去 除此之外,将左孩子的子孙的最大节点提上去. 代码实现: struct node{ int data; n

【数据结构第四周】树知识点整理(下)【二叉搜索树】

二叉搜索树 (1)定义 二叉搜索树(Binary Search Tree),也称二叉排序树或二叉查找树 一棵二叉树,可以为空:如果不为空,满足以下性质: a.非空左子树的所有键值小于其根节点的键值 b.非空右子树的所有键值大于其根节点的键值 c.左右子树都是二叉搜索树 (2)相关操作 Position Find( ElementType X, BinTree BST ):从二叉搜索树BST 中查找元素X,返回其所在结点的地址 Position FindMin( BinTree BST ):从二叉