• 776. 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

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    不知道怎么返回2个二叉树:用TreeNode[]数组,分为0 1两个变量即可

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [一句话思路]:

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. traverse获得的是一个数组,然后splitted[0]一个点可以给tree中的一个点

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    返回2个二叉树:用TreeNode[]数组,分为0 1两个变量即可

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [算法思想:迭代/递归/分治/贪心]:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

     [是否头一次写此类driver funcion的代码] :

     [潜台词] :

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public TreeNode[] splitBST(TreeNode root, int V) {
            //initialization
            TreeNode[] splitted = new TreeNode[2];
            
            //corner case
            if (root == null) return splitted;
            
            //discuss 2 cases
            if (V >= root.val) {
                splitted = splitBST(root.right, V);
                root.right = splitted[0];
                splitted[0] = root;
            }else {
                splitted = splitBST(root.left, V);
                root.left = splitted[1];
                splitted[1] = root;
            }
            
            //return
            return splitted;
        }
    }
    View Code
  • 相关阅读:
    Unlicensed ARC session – terminating!
    ArcGIS读取dem格式数据
    OCIEnvCreate 失败,返回代码为 -1的解决方法
    PowerDesigner设计的数据库 ORA-0092
    Oracle空间查询 ORA-28595
    PowerDesigner添加表注释
    C# 动态解析表达式
    远程桌面不能交互复制粘贴
    ArcGIS10.4 Runtime Error R6034
    ArcGIS Add-in ValidateAddInXMLTask”任务意外失败
  • 原文地址:https://www.cnblogs.com/immiao0319/p/9527950.html
Copyright © 2020-2023  润新知