zoukankan      html  css  js  c++  java
  • 【LeetCode106】Construct Binary Tree from Inorder and Postorder Traversal★★

    1.题目

    2.思路

       思路和LeetCode105类似,见上篇。

    3.java代码

     1 //测试
     2 public class BuildTreeUsingInorderAndPostorder {
     3      public static void main(String[] args) {
     4             int[] postSort={7,4,2,5,8,6,3,1};
     5             int[] inSort={4,7,2,1,5,3,8,6};
     6             System.out.println(new Solution2().buildTree(postSort, inSort));
     7         }
     8 }
     9 //利用中序和后序重建二叉树
    10 class Solution2 {
    11     public TreeNode buildTree(int[] inorder, int[] postorder) {
    12         //参数校验
    13         if(postorder==null||inorder==null||postorder.length!=inorder.length||postorder.length==0) 
    14             return null;
    15         return buildTreeCore(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
    16     }
    17     /**
    18      * 构建二叉树,数据输入的正确性由输入数据自己保证
    19      *
    20      * @param postorder                    后序遍历的结果
    21      * @param startPostorder  后序遍历的开始位置
    22      * @param endPostorder    后序遍历的结束位置
    23      * @param inorder        中序遍历的结果
    24      * @param startInorder   中序遍历的开始位置
    25      * @param endInorder     中序遍历的结束位置
    26      * @return 二叉树的根结点
    27      */
    28     private TreeNode buildTreeCore(int[] inorder,int startInorder, int endInorder,
    29             int[] postorder, int startPostorder, int endPostorder) {
    30         // 只有一个元素时直接返回该节点,这也是递归结束的出口标志
    31         if(startPostorder==endPostorder){
    32             return new TreeNode(postorder[endPostorder]);
    33         }else{
    34             // 记录根结点的在中序遍历中的位置
    35             int rootIn=startInorder;
    36             for(int i=startInorder;i<=endInorder;i++){
    37                 if(inorder[i]==postorder[endPostorder]){
    38                     rootIn=i;
    39                     break;
    40                 }
    41             }
    42             // 创建根结点
    43             TreeNode root=new TreeNode(inorder[rootIn]);
    44              // 左子树的结点个数
    45             int leftLength=rootIn-startInorder;
    46             if(leftLength>0){
    47                 // startPostorder, startPostorder+leftLength-1:左子树在后序序列中的起始和结束位置
    48                 root.left=buildTreeCore(inorder, startInorder, rootIn-1, postorder, startPostorder, startPostorder+leftLength-1);
    49             }
    50             // 右子树的结点个数
    51             int rightLength=endInorder-rootIn;
    52             if(rightLength>0){
    53                 // startPostorder+leftLength, endPostorder:左子树在后序序列中的起始和结束位置
    54                 root.right=buildTreeCore(inorder, rootIn+1, endInorder, postorder, startPostorder+leftLength, endPostorder-1);
    55             }
    56             return root;
    57         }
    58     }
    59 }
    60 //二叉树节点定义
    61 class TreeNode {
    62      int val;
    63      TreeNode left;
    64      TreeNode right;
    65      TreeNode(int x) { val = x; }
    66 }
  • 相关阅读:
    164-268. 丢失的数字
    163-20. 有效的括号
    Sword 30
    Sword 29
    Sword 27
    Sword 25
    Sword 24
    Sword 22
    Sword 21
    Sword 18
  • 原文地址:https://www.cnblogs.com/zhangboy/p/6558563.html
Copyright © 2011-2022 走看看