LeetCode【7】树的构造

刷了两个题,都不是很熟,所以记录在博客上

https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/

https://leetcode-cn.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/

直接上代码

import sun.reflect.generics.tree.Tree;

/**
 * @Author 宋宗垚
 * @Date 2018/11/27 15:50
 * @Description 从中序与后序遍历序列构造二叉树
 */
/*
根据一棵树的中序遍历与后序遍历构造二叉树。

注意:
你可以假设树中没有重复的元素。

例如,给出

中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:

    3
   / \
  9  20
    /  \
   15   7
 */
/*
中序遍历:左根右
后序遍历  左右根
 */
public class ConstructBinaryTreefromInorderandPostorderTraversal_106 {
    public TreeNode buildTree(int[] inorder, int[] postorder) {

        return creatTree(inorder,0,inorder.length-1,postorder,0, postorder.length-1);
    }
    public TreeNode creatTree(int[] inorder,int iLeft,int iRight,int[] postorder,int pLeft,int pRight){
        if(iLeft>iRight || pLeft>pRight){
            return null;
        }
        TreeNode root = new TreeNode(postorder[pRight]);
        //root.val = postorder[pRight];
        int index = 0;
        for(int i=iLeft;i<=iRight;i++){
            if(inorder[i]==root.val){
                index = i;
                break;
            }
        }
        int leftcount = index-iLeft;
        root.left = creatTree(inorder,iLeft,index-1,postorder,pLeft,pLeft+leftcount-1);
        root.right = creatTree(inorder,index+1,iRight,postorder,pLeft+leftcount,pRight-1);

        return root;
    }

}
import java.util.Arrays;
import java.util.Collections;

/**
 * @Author 宋宗垚
 * @Date 2018/11/27 14:09
 * @Description 从前序与中序遍历序列构造二叉树
 */
/*
根据一棵树的前序遍历与中序遍历构造二叉树。

注意:
你可以假设树中没有重复的元素。

例如,给出

前序遍历 preorder = [3,9,20,15,7]
中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:

    3
   / \
  9  20
    /  \
   15   7
 */
public class ConstructBinaryTreefromPreorderandInorderTraversal_105 {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder == null || preorder.length == 0) { return null; }
        return buildTree(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
//        return buildTree1(preorder,inorder);

    }

    public TreeNode buildTree(int[] preorder,int pLeft,int pRight,int[] inorder,int iLeft,int iRight){
        if(pLeft>pRight || iLeft> iRight){
            return null;
        }
        TreeNode a = new TreeNode(preorder[pLeft]);
        // 查找前序遍历中的最前面的那个数,在中序遍历中的位置
        int index =find(inorder,iLeft,iRight,a.val);

        int lengthOfLeft = index-iLeft; // 当前节点左子树的节点个数
//        int[] x = Arrays.copyOfRange(preorder,0,preorder.length-1);
        a.left = buildTree(preorder,pLeft+1,pLeft+1+lengthOfLeft-1,inorder,iLeft,index-1);
        a.right = buildTree(preorder,pLeft+1+lengthOfLeft,pRight,inorder,index+1,iRight);
        return a;

    }

    public TreeNode c(int[] pre,int pLeft,int pRight,int[] inorder,int iLeft,int iRight){
        if(pLeft>pRight || iLeft>iRight){
            return null;
        }
        TreeNode a = new TreeNode(pre[pLeft]);
        int index =find(inorder,iLeft,iRight,a.val);
        int count = index - iLeft;
        a.left = c(pre,pLeft+1,pLeft+1+count-1,inorder,iLeft,index-1);
        a.right = c(pre,pLeft+1+count,pRight,inorder,index+1,iRight);
        return a;



    }
    public int find(int[] a,int left,int right,int targer){
        int result = -1;
        for(int i = left;i<=right;i++){
            if(a[i]==targer){
                result=i;
                break;
            }
        }
        return result;
    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值