Leetcode105 Construct Binary Tree from Preorder and Inorder Traversal Java实现

Posted chason95

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode105 Construct Binary Tree from Preorder and Inorder Traversal Java实现相关的知识,希望对你有一定的参考价值。

先写了一个最原始的方法1:(java没有切片很难受啊)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        int len = inorder.length;
        if(len==0) return null;
        TreeNode p = new TreeNode(preorder[0]);
        if(len==1) return p;
        
        int index=0;for(;index<len;index++) {if(inorder[index]==preorder[0]) break;}
        if(index==0) {    //说明只有右子树
            int[] newPre = new int[len-1];int[] newIn = new int[len-1];
            for(int i=0;i<len-1;i++) {
                newPre[i] = preorder[i+1];newIn[i]=inorder[i+1];
            }
            p.right = buildTree(newPre,newIn);
            return p;
        }
        else if(index==len-1) {
            int[] newPre = new int[len-1];int[] newIn = new int[len-1];
            for(int i=0;i<len-1;i++) {
                newPre[i] = preorder[i+1];newIn[i]=inorder[i];
            }
            p.left = buildTree(newPre,newIn);
            return p;
        }
        else {
            int[] lp = new int[index];int[] li = new int[index];
            int[] rp = new int[len-index-1];int[] ri= new int[len-1-index];
            for(int i=0;i<index;i++) {
                lp[i] = preorder[i+1];li[i]=inorder[i];
            }
            for(int i=index+1;i<len;i++) {
                rp[i-index-1]=preorder[i];ri[i-index-1]=inorder[i];
            }
            p.left = buildTree(lp,li);
            p.right = buildTree(rp,ri);
            return p;
        }
    }
}

技术分享图片

效率低的令人发指,不过想想也是,每次都要重新建立数组,肯定麻烦啊,于是优化,得到方法2:

class Solution {
    public TreeNode buildTree(int[] preorder,int[] inorder) {
        return build(preorder,inorder,0,preorder.length-1,0,inorder.length-1);
    }
    public TreeNode build(int[] preorder,int[] inorder,int pl,int pr,int il,int ir) {
        int len = pr-pl+1;
        if(len==0) return null;
        TreeNode p = new TreeNode(preorder[pl]);
        if(len==1) return p;
        
        int index=il;for(;index<ir+1;index++) {if(inorder[index]==preorder[pl]) break;}
        p.left = build(preorder,inorder,pl+1,pl+index-il,il,index-1);
        p.right = build(preorder,inorder,pl+index-il+1,pr,index+1,ir);
        return p;
    }
}

技术分享图片

效果好了一些,可依旧不在第一梯队。

怎么优化呢?查看大神思路发现,每次都要在inorder序列中搜索一遍,效率较低,可以建立一个Map存储inorder中值与index的对应关系,提高效率:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] preorder,int[] inorder) {
        Map<Integer,Integer> indexMap = new HashMap<>();
        for(int i=0;i<inorder.length;i++) indexMap.put(inorder[i],i);
        return buildv2(preorder,inorder,0,preorder.length-1,0,inorder.length-1,indexMap);
    }
    public TreeNode buildv2(int[] preorder,int[] inorder,int pl,int pr,int il,int ir,Map<Integer,Integer> indexMap) {
        int len = pr-pl+1;
        if(len==0) return null;
        TreeNode p = new TreeNode(preorder[pl]);
        if(len==1) return p;
        
        int index=indexMap.get(preorder[pl]);
        p.left = buildv2(preorder,inorder,pl+1,pl+index-il,il,index-1,indexMap);
        p.right = buildv2(preorder,inorder,pl+index-il+1,pr,index+1,ir,indexMap);
        return p;
    }
}

技术分享图片

进入第一梯队,1ms大佬的解法没看太明白,留待以后再看吧。

以上是关于Leetcode105 Construct Binary Tree from Preorder and Inorder Traversal Java实现的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode-105-Construct Binary Tree from Preorder and Inorder Traversal

一天一道LeetCode#105. Construct Binary Tree from Preorder and Inorder Traversal

LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal

leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java

leetcode105:Construct Binary Tree from Preorder and Inorder Traversal

LeetCode105 Construct Binary Tree from Preorder and Inorder Traversal