• 【LeetCode】105.从前序与中序遍历序列构造二叉树(含参数少的解法)


    题目地址:https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

    题目:

    1. 从前序与中序遍历序列构造二叉树
      根据一棵树的前序遍历与中序遍历构造二叉树。

    注意:
    你可以假设树中没有重复的元素。
    例如,给出
    前序遍历 preorder = [3,9,20,15,7]
    中序遍历 inorder = [9,3,15,20,7]
    返回如下的二叉树:

        3
       / 
      9  20
        /  
       15   7
    

    分析:

    二叉树相关的很多问题的解决思路都有分治法的思想在里面。我们复习一下分治法的思想:把原问题拆解成若干个与原问题结构相同但规模更小的子问题,待子问题解决以后,原问题就得以解决,“归并排序”和“快速排序”都是分治法思想的应用,其中“归并排序”先无脑地“分”,在“合”的时候就麻烦一些;“快速排序”开始在 partition 上花了很多时间,即在“分”上使了很多劲,然后就递归处理下去就好了,没有在“合”上再花时间。

    抓住“前序遍历的第 1 个元素一定是二叉树的根结点”,不难写出代码。关键还是拿 LeetCode 上面的例子画一个图,思路就很清晰了。

    前序遍历数组的第 11 个数(索引为 00)的数一定是二叉树的根结点,于是可以在中序遍历中找这个根结点的索引,然后把“前序遍历数组”和“中序遍历数组”分为两个部分,就分别对应二叉树的左子树和右子树,分别递归完成就可以了。

    在这里插入图片描述

    下面是一个具体的例子,演示了如何计算数组子区间的边界:
    在这里插入图片描述

    代码

    import java.util.HashMap;
    import java.util.Map;
    
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
    
        TreeNode(int x) {
            val = x;
        }
    }
    
    public class Solution {
    
        private int[] preorder;
        private Map<Integer, Integer> hash;
    
        public TreeNode buildTree(int[] preorder, int[] inorder) {
            int preLen = preorder.length;
            int inLen = inorder.length;
            if (preLen != inLen) {
                throw new RuntimeException("Incorrect input data.");
            }
            this.preorder = preorder;
            this.hash = new HashMap<>();
            for (int i = 0; i < inLen; i++) {
                hash.put(inorder[i], i);
            }
    
            return buildTree(0, preLen - 1, 0, inLen - 1);
        }
    
    
        private TreeNode buildTree(int preLeft, int preRight, int inLeft, int inRight) {
            // 因为是递归调用的方法,按照国际惯例,先写递归终止条件
            if (preLeft > preRight || inLeft > inRight) {
                return null;
            }
            // 先序遍历的起点元素很重要
            int pivot = preorder[preLeft];
            TreeNode root = new TreeNode(pivot);
            int pivotIndex = hash.get(pivot);
            root.left = buildTree(preLeft + 1, pivotIndex - inLeft + preLeft,
                    inLeft, pivotIndex - 1);
            root.right = buildTree(pivotIndex - inLeft + preLeft + 1, preRight,
                    pivotIndex + 1, inRight);
            return root;
        }
    }
    

    ##传递参数少的方法

    class solution{
     Map<Integer, Integer> map = new HashMap<>();
            int psize = 0;
    
            public TreeNode buildTree(int[] preorder, int[] inorder) {
                for (int i = 0; i < inorder.length; i++) {
                    map.put(inorder[i], i);
                }
                return build(preorder, 0, preorder.length - 1);
            }
    
            public TreeNode build(int[] pre, int inl, int inr) {
                if (inl > inr) return null;
                TreeNode node = new TreeNode(pre[psize++]);
                int index = map.get(node.val);
                node.left = build(pre, inl, index - 1);
                node.right = build(pre, index + 1, inr);
                return node;
            }
        }
    
    
    
  • 相关阅读:
    语料和文本处理
    seq2seq+torch7聊天机器人bug处理
    unity3d inputfield标签控制台打印object
    多种语言tcp编程
    处理json中的空格
    安卓无法访问Azure服务器和微软API
    Xamarin/Unity3d无法访问Azure服务器或者微软API
    unity3d C# soket客户端接受失败
    unity3d之public变量引发错误
    unity3d,java,c#,python,rospy的socket通信测试
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13308077.html
Copyright © 2020-2023  润新知