原题链接在这里:https://leetcode.com/problems/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   7while 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.

题解:

If current node value is larger than V, then perfor DFS on its left subtree.

The returned value is an array of 2 trees. First one is smaller than or equal to V, second is larger than V.

Then current node left child should be first one. Now current node including its left subtree are all smaller than or equal to V, use it as new first element of array, and existing second element to return.

Vice versa.

Time Complexity: O(h).

Space: O(h).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public TreeNode[] splitBST(TreeNode root, int V) {
12         if(root == null){
13             return new TreeNode[]{null, null};
14         }
15
16         if(root.val > V){
17             TreeNode [] left = splitBST(root.left, V);
18             root.left = left[1];
19             return new TreeNode[]{left[0], root};
20         }else{
21             TreeNode [] right = splitBST(root.right, V);
22             root.right = right[0];
23             return new TreeNode[]{root, right[1]};
24         }
25     }
26 }

类似Delete Node in a BST.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11145009.html

LeetCode 776. Split BST相关推荐

  1. LeetCode 333. 最大 BST 子树(递归)*

    文章目录 1. 题目 2. 解题 1. 题目 给定一个二叉树,找到其中最大的二叉搜索树(BST)子树, 其中最大指的是子树节点数最多的. 注意: 子树必须包含其所有后代. 示例: 输入: [10,5, ...

  2. Leetcode——538. Convert BST to Greater Tree

    题目原址 https://leetcode.com/problems/convert-bst-to-greater-tree/description/ 题目描述 Given a Binary Sear ...

  3. LeetCode——树:BST

    BST 目录 概述 修剪二叉查找树(LeetCode669) 寻找二叉查找树的第k个元素(LeetCode230) 把二叉查找树每个节点的值都加上比它大的节点的值 二叉查找树的最近公共祖先(LeetC ...

  4. leetcode 725. Split Linked List in Parts | 725. 分隔链表(Java)

    题目 https://leetcode.com/problems/split-linked-list-in-parts/ 题解 hint: If there are N nodes in the li ...

  5. LeetCode 410. Split Array Largest Sum

    方法一:DFS+Memoization 某种程度来说本题和 Word Break 很像.直接dfs暴力做肯定会超时,加上memoization即可. dfs(start, m, ...) 表示从ind ...

  6. Leetcode 538 - Convert BST to Greater Tree

    题意 给一个BST,改变其节点的值,将其节点的值加上所有比他大的节点的值 思路 首先注意是BST,其中序遍历的节点是递增序列.即假设中序遍历后的结果是a0,a1,a2,....ana_0, a_1, ...

  7. Golang Leetcode 538. Convert BST to Greater Tree.go

    思路 因为给定的树是BST,所以可以知道如果采用后续遍历的方式,第一个找到的节点应该是最大的节点.所以采用递归的方式从最大的节点开始改变树的值 code var sum intfunc convert ...

  8. leetcode 659. Split Array into Consecutive Subsequences

    原题: You are given an integer array sorted in ascending order (may contain duplicates), you need to s ...

  9. LeetCode 659 Split Array into Consecutive Subsequences

    LeetCode Brute Force的方法就是把维护这些字串,每次来新的就加入到符合条件的但是最短的字串中去. 因为不需要求这些字串,可以做一些优化,用字串最后一位数作为key,把长度存入一个di ...

  10. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

最新文章

  1. 解决ajax获取不到按钮的id
  2. 深度学习100例 | 第24天-卷积神经网络(Xception):动物识别
  3. Java中三种交换值得方式
  4. java随机安排座位表程序_java – 为长凳生成随机座位表的最有效算法?
  5. SQLServer 批量插入数据的两种方法
  6. IREC-GAN:在线推荐中基于模型的对抗训练强化学习
  7. Xamarin Android 应用程序内图标上数字提示
  8. Java程序猿修炼之道 之 Logging(3/3) - 怎么分析Log
  9. 2019 第四季度计划
  10. LINUX下载编译opusfile/opus-tools
  11. SIM800 发送http请求AT指令
  12. 基于Java+SpringBoot+Thymeleaf+Mysql多用户B2C商城平台系统设计与实现
  13. 用户标签照妖镜api、旺旺搜索搜索标签api、用户标签查询api、淘宝用户标签透视透视api
  14. ffmpeg解码h265,并用Qt显示
  15. 码农自述:猝死瞬间,我在想些什么?
  16. 速学TypeScript-精简划重点手册-上册
  17. mes系统故障_mes系统实施失败是什么原因?
  18. 《九日集训》打卡第二天
  19. favicon.ico介绍,网页图标的制作动态网页图标
  20. 30个计算机相关的英语词汇,计算机应用常用英语词汇30

热门文章

  1. Spring Batch 4.2.0.M1 发布,批处理应用编写框架
  2. FastHook——实现.dynsym段和.symtab段符号查询
  3. CDH5 Hadoop如何支持读写OSS
  4. css的布局模型(三)—层模型
  5. DWR入门教程(http://www.cnblogs.com/cyjch/archive/2012/02/16/2353758.html)
  6. HTTP协议 通信过程介绍
  7. Java网络编程从入门到精通(24):实现HTTP断点续传下载工具(附源代码)
  8. python数据包之利器scapy用法!
  9. Django 系列博客(二)
  10. 20165301课下作业