题目

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

Note:

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

Show Tags

Show Similar Problems

分析

跟上一道题同样的道理。

AC代码

class Solution {
public: template <typename Iter>
TreeNode* make(Iter in_begin, Iter in_end , Iter post_begin, Iter post_end ) { if (post_begin == post_end || in_begin == in_end)
return NULL; int size = post_end - post_begin;
//后序遍历最后一个节点为树的根节点
TreeNode *root = new TreeNode(*(post_begin + size-1)); //在中序遍历结果中查找根节点
Iter iter = find(in_begin, in_end, *(post_begin + size - 1)); //计算左子树个数
int count = iter - in_begin; if (iter != in_end)
{
//则在inOrder中(0 , count-1)为左子树中序遍历结果(count+1,size-1)为右子树的中序遍历序列
//在preOrder中(0,count-1)为左子树前序遍历结果(count,size-2)为右子树前序遍历结果 root->left = make(in_begin, iter , post_begin, post_begin + count); //构造右子树
root->right = make(iter + 1, in_end ,post_begin + count, post_begin + size - 1);
}
return root; } TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.empty() || postorder.empty())
return NULL; return make(inorder.begin(), inorder.end() ,postorder.begin(), postorder.end());
} };

GitHub测试程序源码

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

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

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

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

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

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

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

  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 Week14]Construct Binary Tree from Inorder and Postorder Traversal

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

  8. leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

    1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...

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

随机推荐

  1. github添加版本号

    1.将官方的库clone下来 http://github.com/xxx.xxx.git 2.修改要修改的地方 3.git add src 4.commit -m 'xxx'   5.git push ...

  2. 基于.net core微服务(Consul、Ocelot、Docker、App.Metrics+InfluxDB+Grafana、Exceptionless、数据一致性、Jenkins)

    1.微服务简介 一种架构模式,提倡将单一应用程序划分成一组小的服务,服务之间互相协调.互相配合,为用户提供最终价值.每个服务运行在其独立的进程中,服务与服务间采用轻量级的通信机制互相沟通(RESTfu ...

  3. JAVA常用知识总结(十一)——数据库(一)

    项目中用到的不常见sql语法 1:空值不在前的排序 select a.* from WZX_SCZY A order by SCZY_START_TIME desc nulls last (不加nul ...

  4. 关于ViewPager高度自适应(随着pager页的高度改变Viewpager的高度)

    该博客借鉴的是某位大神的博客 我只是写一下用后感和总结 博客链接地址 http://blog.csdn.net/qq_34972666/article/details/52386999?locatio ...

  5. STM32的低功耗模式

    一 待机模式standby和STOP模式的区别: 进入低功耗模式:都一样,都是先关闭相应时钟,关闭相应外设,配置相应所有IO口(浮动输入),然后配置相应的唤醒中断源,中断影响的O口,然后调用相应函数进 ...

  6. HDU 1028 Ignatius and the Princess III dp整数划分

    http://acm.hdu.edu.cn/showproblem.php?pid=1028 dp[i][j]表示数值为i,然后最小拆分的那个数是j的时候的总和. 1 = 1 2 = 1 + 1 . ...

  7. HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景

    http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...

  8. Python相对导入导致SystemError的解决方案(译)

    原文出处: http://stackoverflow.com/   译文出处:yibohu1899 这个问题是如何解决在相对导入的时候,如果出现’System Error’的时候的解决方案.顺带一提, ...

  9. JVM垃圾回收机制一

    JVM内存分配与回收 JVM 分代 JVM把堆分为年轻代和老年代,年轻代又分为1个Eden区和2个Survivor区,Eden和Survivor的内存的大小比例是8:1:1. 为什么要分代? 很大的原 ...

  10. FPGA的嵌入式RAM

    FPGA中的嵌入式RAM分为两种:专用的BRAM和分布是RAM(用LUT实现的).这两种RAM又可以配置成单端口和双端口的RAM和ROM.双端口RAM又可以根据读写地址是否在同一块分为Double P ...