力扣面试150 从中序与后序遍历序列构造二叉树 递归-???? Code

时间:2024-10-15 07:18:00

在这里插入图片描述

/**
 * 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 {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        int n = postorder.length;
        if(n == 0){
            return null;
        }
        int leftSize = indexOf(inorder, postorder[n-1]);// 左区间的长度
        int[] in1 = Arrays.copyOfRange(inorder, 0, leftSize);
        int[] in2 = Arrays.copyOfRange(inorder, leftSize+1, n);// 记得排除中间的根节点
        int[] post1 = Arrays.copyOfRange(postorder, 0, leftSize);
        int[] post2 = Arrays.copyOfRange(postorder, leftSize, n-1);// 排除最后一个根节点
        TreeNode left = buildTree(in1, post1);
        TreeNode right = buildTree(in2,post2);
        return new TreeNode(postorder[n-1],left,right);
    }

    // 返回 x 在 a 中的下标,保证 x 一定在 a 中
    private int indexOf(int[] a , int x){
        for(int i = 0; ; i++){
            if(a[i] == x){
                return i;
            }
        }
    }
}