剑指 Offer 07 重建二叉树

题目:给前序和中序二叉树序列,建立二叉树(数据都不重复)

主方法声明:public TreeNode buildTree(int[] preorder, int[] inorder)

思路:使用递归方法build()

private TreeNode build(int[] preorder, int preorderStart, int preorderEnd, int[] inorder, int inorderStart, int inorderEnd, Map<Integer, Integer> indexMap)

因为树的操作大部分都要使用递归。map是的key是放inorder里的值,value是放inorder对应的下标。方便通过值在inorder里找到rootIndex。
把root.left = build(…)和root.right = build(…)写好。
build递归结束的条件是当preorderStart超过preorderEnd。因为preorderStart不断往右走,就不断在inorder里确定子树的根。把整体串起来。

	public TreeNode buildTree(int[] preorder,int[] inorder){
        if(preorder == null || inorder == null || preorder.length != inorder.length){
            return null;
        }
        //map用来快速通过inorder的值来得到下标(为rootIndex)
        Map<Integer,Integer> indexMap = new HashMap<>();
        for (int i = 0; i < inorder.length; i++) {
            indexMap.put(inorder[i],i);
        }
        TreeNode root = build(preorder,0,preorder.length-1,inorder,0,inorder.length-1,indexMap);
        return root;
    }

    private TreeNode build(int[] preorder, int preorderStart, int preorderEnd, int[] inorder, int inorderStart, int inorderEnd, Map<Integer, Integer> indexMap) {
        //build递归结束的条件(preorderStart不断往右走,不断在inorder里确定子树的根)
        if(preorderStart > preorderEnd){
            return null;
        }
        TreeNode root = new TreeNode(preorder[preorderStart]);
        int rootIndex = indexMap.get(root.val);
        root.left = build(preorder,preorderStart+1,preorderStart+rootIndex-inorderStart,inorder,inorderStart,rootIndex-1,indexMap);
        root.right = build(preorder,preorderStart+rootIndex-inorderStart+1,preorderEnd,inorder,rootIndex+1,inorderEnd,indexMap);
        return root;
    }

扩展:给后序和中序,自己写一遍
Leetcode 106题
思路:postorder序列最右边的是root。只需要把root.left和root.right重写一下,其它不变。

	public TreeNode buildTree(int[] postorder,int[] inorder){
        if(postorder == null || inorder == null || postorder.length != inorder.length){
            return null;
        }
        //map用来快速通过inorder的值来得到下标(为rootIndex)
        Map<Integer,Integer> indexMap = new HashMap<>();
        for (int i = 0; i < inorder.length; i++) {
            indexMap.put(inorder[i],i);
        }
        TreeNode root = build(postorder,0,postorder.length-1,inorder,0,inorder.length-1,indexMap);
        return root;
    }

    private TreeNode build(int[] postorder, int postorderStart, int postorderEnd, int[] inorder, int inorderStart, int inorderEnd, Map<Integer, Integer> indexMap) {
        if(postorderStart > postorderEnd){
            return null;
        }
        //后序遍历中最右边那个节点为root
        TreeNode root = new TreeNode(postorder[postorderEnd]);
        int rootIndex = indexMap.get(root.val);
        root.left = build(postorder,postorderStart,postorderStart+rootIndex-inorderStart-1,inorder,inorderStart,rootIndex-1,indexMap);
        root.right = build(postorder,postorderStart+rootIndex-inorderStart,postorderEnd-1,inorder,rootIndex+1,inorderEnd,indexMap);
        return root;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值