zoukankan      html  css  js  c++  java
  • 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree.

    Note:
    You may assume that duplicates do not exist in the tree.

    For example, given

    inorder = [9,3,15,20,7]
    postorder = [9,15,7,20,3]

    Return the following binary tree:

        3
       / 
      9  20
        /  
       15   7
    
     1 class Solution {
     2     public TreeNode buildTree(int[] inorder, int[] postorder) {
     3         return help(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
     4     }
     5     private TreeNode help(int[] inorder,int in_start,int in_end,int[] postorder,int post_start,int post_end){
     6         if(in_start>in_end||post_start>post_end) return null;
     7         TreeNode root = new TreeNode(postorder[post_end]);
     8         int j = in_start;
     9         while(inorder[j]!=postorder[post_end]&& j <=in_end)
    10             j++;
    11         root.left = help(inorder,in_start,j-1,postorder,post_start,post_start+(j-in_start)-1);
    12         root.right = help(inorder,j+1,in_end,postorder,post_end-(in_end-j),post_end-1);
    13         return root;
    14     }
    15 }
  • 相关阅读:
    电脑不能连接到热点
    常用网络协议
    HVV面试
    【转载】hacker术语
    渗透测试学习路线
    系统安全——可信计算
    rsync文件同步详解
    rabbitmq集群部署高可用配置
    ansible自动化部署之路笔记
    ELK-elasticsearch-6.3.2部署
  • 原文地址:https://www.cnblogs.com/zle1992/p/8379513.html
Copyright © 2011-2022 走看看