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

题目大意:给定一个二叉树的中序和后续序列,构建出这个二叉树。

解题思路:首先后序序列的最后一个是根节点,然后在中序序列中找到这个节点,中序序列中这个节点左边的是根节点的左子树,右边的是右子树,由此递归构建出完整的树。

Talk is cheap:

    public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder == null || postorder == null) {
return null;
}
int inLen = inorder.length;
int postLen = postorder.length;
if ((inLen == 0 && postLen == 0) || inLen != postLen) {
return null;
} TreeNode root = new TreeNode(postorder[postLen - 1]);
if (inLen == 1) {
return root;
}
int pos = 0;
for (int i = 0; i < inLen; i++) {
if (inorder[i] == postorder[postLen - 1]) {
pos = i;
break;
}
}
int[] inLeft = Arrays.copyOfRange(inorder, 0, pos);
int[] inRight = Arrays.copyOfRange(inorder, pos + 1, inLen);
int[] postLeft = Arrays.copyOfRange(postorder, 0, pos);
int[] postRight = Arrays.copyOfRange(postorder, pos, postLen - 1); root.left = buildTree(inLeft, postLeft);
root.right = buildTree(inRight, postRight);
return root;
}

Construct Binary Tree from Inorder and Postorder Traversal——LeetCode的更多相关文章

  1. Construct Binary Tree from Inorder and Postorder Traversal || LeetCode

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...

  2. 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...

  3. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  4. 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

  5. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  6. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  7. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  8. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  9. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

随机推荐

  1. 安装jdk后出现bash: ./java: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录

    用sudo yum install glibc.i686命令安装好glibc之后问题就解决了

  2. 快速打出System.out.println("");

    sysout---->Alt+/--->System.out.println():

  3. U3D 实现子弹发射效果

    首先,这里子弹要模拟的相似的话,用2D刚体比较好,会有重力,自由落体运动. using UnityEngine; using System.Collections; public class gun ...

  4. PTHREAD_MUTEX_INITIALIZER问题

      PTHREAD_MUTEX_INITIALIZER 与 expected expression before ‘{’ token 在进行 Posix thread 编程时,出现以下编译错误:err ...

  5. iOS中常用的正则表达式

    iOS常用正则表达式 正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹配中文还真是个 ...

  6. 十二、C# 委托与Lambda表达式(匿名方法的另一种写法)

    委托与Lambda表达式   1.委托概述 2.匿名方法 3.语句Lambda 4.表达式Lambda 5.表达式树   一.委托概述 相当于C++当中的方法指针,在C#中使用delegate 委托来 ...

  7. angularJS中如何写自定义指令

    指令定义 对于指令,可以把它简单的理解成在特定DOM元素上运行的函数,指令可以扩展这个元素的功能 例如,ng-click可以让一个元素能够监听click事件,并在接收到事件的时候执行angularJS ...

  8. Atom package安装失败的解决方案

    cd ~/.atom/package git clone [package url] cd [package name] apm install [package name] if lack some ...

  9. js字符串倒序

    有的时候我们需要把字符串倒序. 比如“范坚强”的倒序就是“强坚范”. 如何对字符串进行倒序呢?你首先想到的方法就是生成一个栈,从尾到头依次取出字符串中的字符压入栈中,然后把栈连接成字符串. var r ...

  10. POJ1611-The Suspects-ACM

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 23002   Accepted: 11171 De ...