首页 > 其他分享 >力扣106 从中序与后序遍历序列构造二叉树

力扣106 从中序与后序遍历序列构造二叉树

时间:2023-02-02 23:23:34浏览次数:52  
标签:后序 inorder 力扣 二叉树 数组 106 节点 postorder

题目:

给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

示例:

输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出:[3,9,20,null,null,15,7]

思路:

1.空节点

2.只有根节点

3.找到根节点作为切割点

4.切割中序数组

5.切割后序数组

6.递归处理左右区间

本题最需要注意的点:(1)各分割数组边界(2)右中序数组和右后序数组的起始下标要从0开始

class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {////中序:左根右  后序:左右根
        int psize=postorder.length;
        //1.空节点
        if(psize==0){
            return null;
        }
        TreeNode root=new TreeNode(postorder[psize-1]);
        //2.只有根节点
        if(psize==1){
            return root;
        }
        //3.找到根节点作为切割点
        int index;
        for(index=0;index<inorder.length;index++){
            if(inorder[index]==postorder[psize-1]){
                break;
            }
        }
        //4.切割中序数组
        int[] leftInorder=new int[index];//左中序数组
        for(int i=0;i<index;i++){
            leftInorder[i]=inorder[i];
        }
        int[] rightInorder=new int[inorder.length-index-1];//右中序数组
        for(int i=index+1;i<inorder.length;i++){
            rightInorder[i-index-1]=inorder[i];//注意:右区间的起始下标
        }
        //5.切割后序数组(与切割后对应中序数组大小一致)
        int[] leftPostorder=new int[leftInorder.length];//左后序数组、
        for(int i=0;i<index;i++){
            leftPostorder[i]=postorder[i];
        }
        int[] rightPostorder=new int[rightInorder.length];//右后序数组
         for(int i=index;i<psize-1;i++){
            rightPostorder[i-index]=postorder[i];//注意:右区间的起始下标
        }
        //6.递归处理左右区间
        root.left=buildTree(leftInorder,leftPostorder);
        root.right=buildTree(rightInorder,rightPostorder);
        return root;
    }
}

 

标签:后序,inorder,力扣,二叉树,数组,106,节点,postorder
From: https://www.cnblogs.com/cjhtxdy/p/17087723.html

相关文章

  • 二叉树的不同形态
    题目简介给定二叉树T(树深度H<=10,深度从1开始,结点个数N<1024,结点编号1~N)的层次遍历序列和中序遍历序列,输出T从左向右叶子结点以及二叉树先序和后序遍历序列。输入格式输......
  • LeetCode 对称二叉树算法题解 All In One
    LeetCode对称二叉树算法题解AllInOne对称二叉树原理图解101.SymmetricTree对称二叉树https://leetcode.com/problems/symmetric-tree/https://leetcode.c......
  • 力扣49. 字母异位词分组
    给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只......
  • leetcode-543二叉树直径
    //leetcodesubmitregionbegin(Prohibitmodificationanddeletion)/***Definitionforabinarytreenode.*publicclassTreeNode{*intval;*TreeNodeleft;......
  • 二分查找-力扣(Java)
    题目描述给定一个n个元素有序的(升序)整型数组nums和一个目标值target,写一个函数搜索nums中的target,如果目标值存在返回下标,否则返回-1。来源:力扣(LeetCode)链接......
  • 【智能车】RT1064-GPIO输入输出
    输入上拉输入GPIO引脚配置为上拉输入模式,在默认状态下(GPIO引脚无输入),读取得的GPIO引脚数据为1,高电平;上拉输入默认是高电平,外接低电平有影响,故只能用来检测外接低......
  • 新奥赛 1061
    1061:求整数的和与均值时间限制:1000ms      内存限制:65536KB提交数:119446   通过数:59245【题目描述】读入n(1≤n≤10000)个整数,求它们的和与......
  • 1061
    1061:求整数的和与均值时间限制:1000ms      内存限制:65536KB提交数:119446   通过数:59245【题目描述】读入n(1≤n≤10000)个整数,求它们的和......
  • 【DFS】LeetCode 124. 二叉树中的最大路径和
    题目链接124.二叉树中的最大路径和思路一个子树内部的最大路径和=左子树提供的最大路径和+根节点值+右子树提供的最大路径和。即dfs(root.left)+root.val+dfs(r......
  • 二叉树的递归遍历
    二叉树遍历前序遍历staticList<Integer>list=newArrayList<>();//前序遍历publicstaticList<Integer>preorderTraversal(TreeNoderoot){if(......