IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 递归的魅力(树篇)二 -> 正文阅读

[数据结构与算法]递归的魅力(树篇)二

要说的是,关于递归,要去相信递归函数的作用,不要跳入递归,下面来感受下递归的魅力
有些时候 需要去处理边界,但大概框架对了,剩下的都是debug

最大二叉树 Midium

654. 最大二叉树 - 力扣(LeetCode) (leetcode-cn.com)Midium

这题思路是遍历找到数组最大元素,获取下标然后建树,左区间和又区间递归再进行建树

TreeNode* construct(vector<int>&nums,int l,int r){
    //递归的作用便是返回nums数组下标l到r的最大二叉树
        if(l>=r){
            return nullptr;
        }

        int index=-1,maxVal=INT_MIN;
        for(int i=l;i<r;++i){
            if(maxVal<nums[i]){
                index=i;
                maxVal=nums[i];
            }
        }

        TreeNode *root=new TreeNode(maxVal);

        root->left=construct(nums,l,index);
        root->right=construct(nums,index+1,r);

        return root;
    }

    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        if(nums.empty()){
            return nullptr;
        }
        return construct(nums,0,nums.size());
    }

从前序与中序遍历序列构造二叉树Midium

105. 从前序与中序遍历序列构造二叉树 - 力扣(LeetCode) (leetcode-cn.com)Midium
请添加图片描述

TreeNode* build(vector<int>& preorder,int preStart,int preEnd,vector<int>& inorder,int inStart,int inEnd){
    //递归作用为构造前序和中序遍历序列为参数数组的二叉树,并返回根节点
        if(preStart>preEnd){
            return nullptr;
        }

        int rootVal=preorder[preStart];

        int index=0;
        for(int i=inStart;i<=inEnd;++i){
            if(inorder[i]==rootVal){
                index=i;
                break;
            }
        }

        int leftSize=index-inStart;

        TreeNode* root=new TreeNode(rootVal);

        root->left=build(preorder,preStart+1,preStart+leftSize,inorder,inStart,index-1);
        root->right=build(preorder,preStart+leftSize+1,preEnd,inorder,index+1,inEnd);

        return root;
    }                     
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
        if(preorder.empty()){
            return nullptr;
        }
        return build(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1);
    }

从中序与后序遍历序列构造二叉树Midium

106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode) (leetcode-cn.com)Midium

请添加图片描述

TreeNode* build(vector<int>& inorder,int inStart,int inEnd,vector<int>& postorder,int postStart,int postEnd){
        if(inStart>inEnd){
            return nullptr;
        }

        int rootVal=postorder[postEnd];

        int index=0;
        for(int i=inStart;i<=inEnd;++i){
            if(inorder[i]==rootVal){
                index=i;
                break;
            }
        }

        int leftSize=index-inStart;
        TreeNode *root=new TreeNode(rootVal);

        root->left=build(inorder,inStart,index-1,postorder,postStart,postStart+leftSize-1);
        root->right=build(inorder,index+1,inEnd,postorder,postStart+leftSize,postEnd-1);

        return root;
    }

    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        return build(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1);
    }
  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-31 16:53:22  更:2021-07-31 16:53:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/7 19:40:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码