根据中序和后续遍历构建二叉树。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder==null&&postorder==null||(inorder.length==0&&postorder.length==0)) return null;
return build(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
} public TreeNode build(int[] inorder,int inStart,int inEnd,int[] postorder,int postStart,int postEnd){
if(inStart>inEnd||postStart>postStart) return null;
TreeNode root=new TreeNode(postorder[postEnd]); //后续遍历的最后一个是根节点
//在中序遍历中找到根节点的位置
int i;
for( i=inStart;i<=inEnd;i++){
if(inorder[i]==postorder[postEnd]) break;
} root.left=build(inorder,inStart,i-1,postorder,postStart,postStart+i-inStart-1);
root.right=build(inorder,i+1,inEnd,postorder,postStart+i-inStart,postEnd-1);
return root;
}
}

Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)的更多相关文章

  1. [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  2. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  3. [LeetCode] 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 ...

  4. Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树

    题意:根据二叉树的中序遍历和后序遍历恢复二叉树. 解题思路:看到树首先想到要用递归来解题.以这道题为例:如果一颗二叉树为{1,2,3,4,5,6,7},则中序遍历为{4,2,5,1,6,3,7},后序 ...

  5. Construct Binary Tree from Inorder and Postorder Traversal

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

  6. 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 ...

  7. 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 ...

  8. 【题解二连发】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 ...

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

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

随机推荐

  1. 精通CSS+DIV网页样式与布局--页面背景

    上篇博客,我们主要简单的总结了CSS的图片效果,我们这回来讲讲CSS如何对网页的背景进行设置,网页的背景是整个网页的重要组成部分,她直接决定了整个网页的风格和色调.这篇博客简单的总结一下如何用CSS来 ...

  2. Git版本控制:Git高级教程

    http://blog.csdn.net/pipisorry/article/details/50669350 Git有很多命令行参数,使用起来非常方便.可以运行 man git log ,来看一下这 ...

  3. Java-IO之ByteArrayInputStream

    ByteArrayInputStream是字节数组输入流,继承于InputStream.它包含了一个内部缓冲区,该缓冲区包含从流中读取的字节,其实内部缓冲区就是一个字节数组,而ByteArrayInp ...

  4. iOS中 Animation 动画大全 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! iOS开发者交流QQ群: 446310206 1.iOS中我们能看到的控件都是UIView的子类,比如UIButt ...

  5. flume1.4.0 保存文件到hdfs错误调试

    报错如下 解决方案:将FLUME_HOME/lib目录下的jar文件,替换成HADOOP_HOME/share/hadoop/common/lib下版本更新的jar文件

  6. 《java入门第一季》之泛型类引入

    首先用一个例子引入泛型类. 我定义一个Object类: package cn.it_03; public class ObjectTool { private Object obj; public O ...

  7. 在go中使用json作为主要的配置格式

    最近在用go重构,在先前的代码中,我们使用的ini文件进行配置,但是因为很多历史遗留问题,导致配置混乱,维护困难,自然也需要考虑重构了. 通用配置格式 通用的配置格式有很多,常用的就有ini,json ...

  8. Mahout推荐算法之ItemBased

    Mahout推荐之ItemBased 一.   算法原理 (一)    基本原理 如下图评分矩阵所示:行为user,列为item. 图(1) 该算法的原理: 1.  计算Item之间的相似度. 2.  ...

  9. Linux多线程实践(7) --多线程排序对比

    屏障 int pthread_barrier_init(pthread_barrier_t *restrict barrier, const pthread_barrierattr_t *restri ...

  10. TCP状态转换

    最近笔试遇到一个题目:如果tcp建立连接时第三次握手失败,tcp会做何操作?该问题的本质是判断我们对tcp的状态转换是否能有比较深刻的理解.只要理解了下面的状态转换图,很容易回答上述问题. 在此,将& ...