给定一棵树的中序遍历与后序遍历,依据此构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 = [9,3,15,20,7]
后序遍历 = [9,15,7,20,3]
返回如下的二叉树:
    3
   / \
  9  20
    /  \
   15   7
详见:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/

Java实现:

/**
* 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||inorder.length==0||postorder==null||postorder.length==0||inorder.length!=postorder.length){
return null;
}
return buildTree(inorder,0,inorder.length-1,postorder,0,postorder.length-1);
}
private TreeNode buildTree(int[] inorder,int startIn,int endIn,int[] postorder,int startPost,int endPost){
if(startIn>endIn||startPost>endPost){
return null;
}
TreeNode root=new TreeNode(postorder[endPost]);
for(int i=startIn;i<=endIn;++i){
if(inorder[i]==postorder[endPost]){
root.left=buildTree(inorder,startIn,i-1,postorder,startPost,startPost+i-startIn-1);
root.right=buildTree(inorder,i+1,endIn,postorder,startPost+i-startIn,endPost-1);
}
}
return root;
}
}

106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树的更多相关文章

  1. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

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

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

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

  4. LeetCode OJ 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 ...

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

  6. C#解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 ...

  7. 【LeetCode】105 & 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 ...

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

  9. (二叉树 递归) 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 ...

随机推荐

  1. fatal error C1902: 程序数据库管理器不匹配;请检查安装解决

    http://blog.sina.com.cn/s/blog_9f4bc8e301015uhz.html 1.错误提示:VS2008编译错误fatal error C1902: 程序数据库管理器不匹配 ...

  2. C语言system()函数:执行shell命令

    头文件:#include <stdlib.h> 定义函数:int system(const char * string); 函数说明:system()会调用fork()产生子进程, 由子进 ...

  3. [TJOI 2018] XOR

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=5338 [算法] 首先对这棵树进行树链剖分 那么我们就将一个树上的问题转化为一个序列上 ...

  4. Ubuntu 16.04 安装wine

    1.安装源       sudo add-apt-repository ppa:wine/wine-builds       sudo apt-get update 2.安装wine      sud ...

  5. OpenResty创造者

    OpenResty 是一个开源的 Web 平台,用于开发高性能和高动态的 Web 网关或者 Web 应用.OpenResty 最早是为了支持全网搜索引擎周边的相关搜索的 API 接口,后来我们基于 N ...

  6. hibernate 中的拦截器EmptyInterceptor接口功能

    Interceptor接口提供了从会话(session)回调(callback)应用程序(application)的机制, 这种回调机制可以允许应用程序在持久化对象被保存.更新.删除或是加载之前,检查 ...

  7. Azure REST API (5) 中国Azure EA Portal Billing API

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China. EA Portal的管理url是:https://ea.azure ...

  8. tty初探 — uart驱动框架分析

    写在前面: 我们没有讲UART驱动,不过我们认为,只要系统学习了第2期,应该具备分析UART驱动的能力,小编做答疑几年以来,陆陆续续有不少人问到UART驱动怎么写,所以今天就分享一篇深度长文(1700 ...

  9. 动态规划专题 多阶段决策问题 蓝桥杯 K好数

    问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = 4,L = 2的时候,所有K好数为11.13.20.22 ...

  10. SSAS GUID 添加 行计数,非重复计数 等 遇到的莫名其妙的问题

    在基于某个GUID 进行非重复性计数时 需要对GUID 转换类型,如:CAST(ColumnName as varchar(36)) 可参考:http://stackoverflow.com/ques ...