LeetCode 中级 - 从前序与中序遍历序列构造二叉树(105)

news/2025/2/26 4:51:39

一个前序遍历序列和一个中序遍历序列可以确定一颗唯一的二叉树。

       根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(root),  根据中序遍历特点, 知在查找到的根(root) 前边的序列为根的左子树的中序遍历序列,  后边的序列为根的右子树的中序遍历序列。 设在中序遍历序列(InSequence)根前边有left个元素. 则在前序序列(PreSequence)中, 紧跟着根(root)的left个元素序列(即PreSequence[1...left]) 为根的左子树的前序遍历序列, 在后边的为根的右子树的前序遍历序列.而构造左子树问题其实跟构造整个二叉树问题一样,只是此时前序序列为PreSequence[1...left]), 中序序列为InSequence[0...left-1], 分别为原序列的子串, 构造右子树同样, 显然可以用递归方法解决。
原文:https://blog.csdn.net/yunzhongguwu005/article/details/9270085

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
     if(preorder.size()==0||inorder.structize()==0)
         return NULL;
    else
    {
        return buildCore(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1);        
      }
}
    TreeNode *buildCore(vector<int>&preorder,int preSt,int preEnd,vector<int>&inorder,int inSt,int inEnd)
    {
        //前序遍历的第一个节点是根节点root
        int rootValue=preorder[preSt];
        TreeNode *root=new TreeNode(rootValue);
        
        //前序序列只有根节点时
        if(preSt==preEnd)
            return root;
        
        //遍历中序序列找到根节点的位置
        int rootInorder = inSt;
        while(inorder[rootInorder]!=rootValue&&rootInorder<=inEnd)
            rootInorder++;

        //计算左子树的长度
        int leftLength=rootInorder-inSt;
        //前序序列中左子数的最后一个节点
        int leftPreEnd=preSt+leftLength;
        
        //左子树非空
        if(leftLength>0)
        {
            //重建左子树
            root->left=buildCore(preorder,preSt+1,leftPreEnd,inorder,inSt,rootInorder-1);
        }   
        //右子树非空
        if(leftLength<preEnd-preSt)
        {
            //重建右子树
            root->right=buildCore(preorder,leftPreEnd+1,preEnd,inorder,rootInorder+1,inEnd);
        }
        return root;
    }
};

从中序与后序遍历序列构造二叉树 ,和第一种类似...。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        if(inorder.size()==0||postorder.size()==0)
            return NULL;
        else 
            return buildCoreTree(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1);
    }
    
    TreeNode *buildCoreTree(vector<int>&inorder,int inSt,int inEnd,vector<int>&postorder,int postSt,int postEnd)
    {
        int rootValue=postorder[postEnd];
        TreeNode *root=new TreeNode(rootValue);
        if(postEnd==postSt)
            return root;
        
        int rootInorder=inSt;
        while(inorder[rootInorder]!=rootValue&&rootInorder<=inEnd)
            rootInorder++;
        
        int leftLength=rootInorder-inSt;
        int leftPostEnd=postSt+leftLength;
        if(leftLength>0)
        {
            root->left=buildCoreTree(inorder,inSt,rootInorder-1,postorder,postSt,leftPostEnd-1);
        }
        if(leftLength<inEnd-inSt)
        {
            root->right=buildCoreTree(inorder,rootInorder+1,inEnd,postorder,leftPostEnd,postEnd-1);
        }
        return root;
    }
};

 

转载于:https://www.cnblogs.com/-xinxin/p/10603200.html


http://www.niftyadmin.cn/n/1494288.html

相关文章

IDEA 实用插件总结

IDEA Plugin 备份 Date 2017.05.24 key promoter 在操作IDEA功能时会提示对应的快捷键maven helper 分析maven依赖lombok 支持编译logbok注解FindBugs 扫描工程发现代码明显错误告警问题Alibaba Java Coding Guidelines 阿里巴巴代码规范Grep Console IDEA 控制台日志输出,颜色…

oracle不同实例解锁用户,oracle 用户账号解锁linux下新建oracle数据库实例

linux下新建oracle数据库实例1、在Linux服务器的图形界面下&#xff0c;打开一个终端&#xff0c;输入如下的命令&#xff1b;xhost 2、切换到oracle 用户&#xff0c;使用如下的命令&#xff1a;su – oracle3、指定数据库实例的变量与值&#xff0c;假设要建立的数据实例的SI…

记一次grub引导修复

今早起床发现deepin出了问题&#xff0c;还好周六备份过一次&#xff0c;于是直接用镜像覆盖了主分区 但是开机后出现了引导错误&#xff0c;即grub>的命令行界面 参考一个博客&#xff0c;有惊无险的修复了 ls # 出现了很多设备&#xff0c;逐个测试 ls (hd,gpt?)&#x…

Fiddler 断点功能

Fiddler 断点&#xff1a; (1) Fiddler 是以作为代理服务器的方式进行工作的&#xff0c;所以&#xff0c;本地应用与服务器传递的这些数据都会经过 Fiddler&#xff1b;(2) 有的时候&#xff0c;我们希望在传递的中间进行修改后再传递&#xff0c;那么可以使用 Fiddler 的断点…

deepin 下pycharm中文乱码解决

sudo apt-get install fonts-arphic-uming -y

【待补充,数论--莫比乌斯反演】

定义 假设对于数论函数f(n)和F(n)&#xff0c;有以下关系式&#xff1a; \[F(n)\sum _{{d|n}}f(d)\] 则将其默比乌斯反转公式定义为&#xff1a; \[ f(n)\sum _{d|n}\mu (d)F\left({\frac {n}{d}}\right)\] 这个东西有点难&#xff0c;一时半会是学不会了&#xff0c;先留着 我…

JAVA基础整理-21.03Java数字和日期处理

Java数字格式化 DecimalFormat 是 NumberFormat 的一个子类&#xff0c;用于格式化十进制数字。DecimalFormat 类包含一个模式和一组符号 Java大数字运算&#xff08;BigInteger类和BigDecimal类&#xff09; BigInteger 类是针对整型大数字的处理类&#xff0c;而 BigDecimal …

Shell脚本 中运行sudo命令

在shell脚本中有时需要使用sudo进行提权&#xff0c;运行包含这类脚本的文件时通常需要我们在终端输入sudo密码&#xff0c;但是在一些无人值守的应用中显然就不太适合了。本文通过构建一个多用户的ubuntu操作环境&#xff0c;来展示脚本中需要使用sudo命令时的应用场景。 我们…