码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【英雄哥六月集训】第 29天: 分而治之


    系列文章

    【英雄哥六月集训】第 01天:数组

    【英雄哥六月集训】第 02天:字符串

    【英雄哥六月集训】第 03天:排序

    【英雄哥六月集训】第 04天:贪心

    【英雄哥六月集训】第 05天: 双指针

    【英雄哥六月集训】第 06天:滑动窗口

    【英雄哥六月集训】第 07天:哈希

    【英雄哥六月集训】第 08天:前缀和

    【英雄哥六月集训】第 09天:二分查找

    【英雄哥六月集训】第 10天: 位运算

    【英雄哥六月集训】第 11天: 矩阵

    【英雄哥六月集训】第 12天: 链表

    【英雄哥六月集训】第 13天: 双向链表

    【英雄哥六月集训】第 14天: 栈

    【英雄哥六月集训】第 15天: 二叉树

    【英雄哥六月集训】第 16天: 队列

    【英雄哥六月集训】第 17天: 广度优先搜索

    【英雄哥六月集训】第 18天: 树

    【英雄哥六月集训】第 19天: 二叉树

    【英雄哥六月集训】第 20天: 二叉搜索树

    【英雄哥六月集训】第 21天: 堆(优先队列)

    【英雄哥六月集训】第 22天: 有序集合

    【英雄哥六月集训】第 23天: 字典树

    【英雄哥六月集训】第 24天: 线段树

    【英雄哥六月集训】第 25天: 树状数组

    【英雄哥六月集训】第 26天: 并查集

    【英雄哥六月集训】第 27天: 图

    【英雄哥六月集训】第 28天: 动态规划

    【英雄哥六月集训】第 29天: 分而治之

    文章目录

    • 系列文章
    • 分而治之
    • 一、 最大二叉树
    • 二、 根据前序和后序遍历构造二叉树
    • 三、 将子数组重新排序得到同一个二叉查找树的方案数
    • 总结


    分而治之

    在计算机科学中,分治法是建基于多项分支递归的一种很重要的算法范式。字面上的解释是“分而治之”,就是把一个复杂的问题分成两个或更多的相同或相似的子问题,直到最后子问题可以简单的直接求解,原问题的解即子问题的解的合并。
    这个技巧是很多高效算法的基础,如排序算法(快速排序、归并排序)、傅立叶变换(快速傅立叶变换)。

    一、 最大二叉树

    654. 最大二叉树

    /**
     * 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 {
        TreeNode dfs(int[] nums,int l,int r){
            if(l>r){
                return null;
            }
            int maxidx=l;
            for(int i=l+1;i<=r;++i){
                if(nums[i]>nums[maxidx]){
                    maxidx=i;
                }
            }
            TreeNode root = new TreeNode(nums[maxidx]);
            root.left = dfs(nums,l,maxidx-1);
            root.right = dfs(nums,maxidx+1,r);
            return root;
        }
        public TreeNode constructMaximumBinaryTree(int[] nums) {
            TreeNode root = dfs(nums,0,nums.length-1);
            return root;
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    二、 根据前序和后序遍历构造二叉树

    889. 根据前序和后序遍历构造二叉树

    /**
     * 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 {
        TreeNode dfs(int[] preorder,int[] postorder,int prel, int prer,int postl, int postr){
            System.out.printf("%d %d %d %d\n",prel,prer,postl,postr);
            if(prel>prer){
                return null;
            }
            TreeNode now = new TreeNode(preorder[prel]);
            
            if(prel == prer){
                return now;
            }
            int idx = m.get(preorder[prel+1]),len=idx-postl+1;
            
            now.left=dfs(preorder,postorder,prel+1,prel+len,postl,idx);
            now.right=dfs(preorder,postorder,prel+len+1,prer,idx+1,postr-1);           
            return now;
                
        
        }
            
            
        
        Map<Integer,Integer> m = new HashMap();
        public TreeNode constructFromPrePost(int[] preorder, int[] postorder) {
            for(int i=0;i<postorder.length;i++){
                m.put(postorder[i],i);
            }
            return dfs(preorder,postorder,0,preorder.length-1,0,postorder.length-1);
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    三、 将子数组重新排序得到同一个二叉查找树的方案数

    1569. 将子数组重新排序得到同一个二叉查找树的方案数

    class Solution {
        int maxn = 1010;
        long[][] c = new long[maxn][maxn];
        long mod = 1000000007;
        TreeNode insert(TreeNode root,int val){
            if(root==null){
                return new TreeNode(val);
            }
            if(val<root.val){
                root.left = insert(root.left,val);
            } else if(val>root.val){
                root.right = insert(root.right,val);
            }
            return root;
        }
    
        int count(TreeNode root){
            if(root == null){
                return 0;
            }
            return count(root.left)+count(root.right)+1;
        }
    
        long dfs(TreeNode root){
            if(root == null){
                return 1;
            }
            long ans = dfs(root.left)*dfs(root.right)%mod;
            int leftc = count(root.left);
            int rightc = count(root.right);
            int totalc = leftc+rightc;
            ans *= c[totalc][leftc];
            ans %= mod;
            return ans;
        }
        public int numOfWays(int[] nums) {
            int i,j;
            int n = nums.length;
            for(i=0;i<=n;i++){
                for(j=0;j<=i;++j){
                    if(j==0||j==i){
                        c[i][j]=1;
                    }
                    else{
                        c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
                    }
                }
            }
            TreeNode root=null;
            for(i=0;i<nums.length;++i){
                root=insert(root,nums[i]);
            }
            return (int)(dfs(root))-1;
    
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    总结

  • 相关阅读:
    【开题报告】基于uni-app的汽车租赁app的设计与实现
    盘点国内主流数字孪生厂商!你了解几家?
    MarkDown+Hbuilder学习
    100-centos 安装mysql
    0031【Edabit ★☆☆☆☆☆】【使用箭头函数】Using Arrow Functions
    linux 网络 cat /proc/net/dev 查看测试网络丢包情况
    MySQL索引优化
    Java版 求平均年龄01星球有学长若干名, 给出每个学长的年龄, 求01星球学长的平均年龄, 保留小数点后两位
    systemverilog学习 --- 数组操作(二)
    BGRL pyg环境安装
  • 原文地址:https://blog.csdn.net/weixin_39348931/article/details/125548593
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号