zoukankan      html  css  js  c++  java
  • 9 根据中序遍历和后序遍历得到二叉树

    1. public TreeNode InPostCreat(int[] In, int[] Post, int lin, int hin, int lpost, int hpost) {  
    2.         TreeNode root = new TreeNode(0);  
    3.         if (Post.length == 0 || In.length == 0) {  
    4.             return null;  
    5.         }  
    6.         root.val = Post[hpost];  
    7.         int i;  
    8.         for (i = lin; In[i] != Post[hpost]; i++)  
    9.             ;// 得到中序遍历数组的根节点位置  
    10.     
    11.         int lleng = i - lin;  
    12.         int rleng = hin - i;  
    13.     
    14.         if (lleng > 0) {  
    15.             root.leftchild = InPostCreat(In, Post, lin, lin + lleng - 1, lpost, lpost + lleng - 1);  
    16.         } else {  
    17.             root.leftchild = null;  
    18.         }  
    19.         if (rleng > 0) {  
    20.             root.rightchild = InPostCreat(In, Post, hin - rleng + 1, hin, hpost - rleng, hpost - 1);  
    21.         } else {  
    22.             root.rightchild = null;  
    23.         }  
    24.         return root;  
    25.     }  
  • 相关阅读:
    MySQL 批量删除相同前缀的表
    MySQL 命令登录
    MySQL 密码修改
    谷歌浏览器开发者工具截图
    VIM命令图解
    基于环境变量为多用户配置不同的JDK(win)
    Reddit: 只有独生子女才明白的事
    JSONObject与null
    SpringFramework中重定向
    XML修改节点值
  • 原文地址:https://www.cnblogs.com/wwjldm/p/6919054.html
Copyright © 2011-2022 走看看