要求:根据中序和后序遍历序列构建一棵二叉树

代码如下:

 struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x): val(x),left(NULL), right(NULL) {}
}; typedef vector<int>::iterator Iter; TreeNode *buildTreeFromInorderPostorder(vector<int> &inorder, vector<int> &postorder)
{
return buildBinaryTreeResur(inorder.begin(), inorder.end(), postorder.begin(), postorder.end());
} TreeNode *buildBinaryTreeResur(Iter istart, Iter iend, Iter pstart, Iter pend)
{
if (istart == iend || pstart == pend)
return NULL;
int ival = *(pend-);
Iter ptemp = find(istart, iend, ival);
TreeNode *res = new TreeNode(ival);
res->left = buildBinaryTreeResur(istart, ptemp, pstart, pstart+(ptemp-istart));
res->right = buildBinaryTreeResur(ptemp+, iend, pstart+(ptemp-istart), pend-);
return res;
}

LeetCode: 106_Construct Binary Tree from Inorder and Postorder Traversal | 根据中序和后序遍历构建二叉树 | Medium的更多相关文章

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

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

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

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

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

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

  7. [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树

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

  8. LeetCode——Construct Binary Tree from Inorder and Postorder Traversal

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

  9. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构建二叉树)

    根据中序和后续遍历构建二叉树. /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...

随机推荐

  1. python入门(四):字符串、编码、random

    1.字符串 字符串基本有两种类型,str和bytes >>> s="a" >>> type(s) <class 'str'>     ...

  2. linux下redis4.0.2集群部署(利用原生命令)

    一.部署架构如下 每台服务器准备2个节点,一主一从,主节点为另外两台其中一台的主,从节点为另外两台其中一台的从. 二.准备6个节点配置文件 在172.28.18.75上操作 cd /etc/redis ...

  3. flash/flex builder在IE中stage.stageWidth始终为0的解决办法

    这应该是IE的bug,解决办法: 原作者:菩提树下的杨过出处:http://yjmyzz.cnblogs.com stage.align=StageAlign.TOP_LEFT; stage.scal ...

  4. 数组中出现次数超过一半的数字(python)

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

  5. FortiGate防火墙500D下PC至外网丢包

    1.现状: 如图,防火墙堆叠,500D共4个出口方向,联通.电信.FQ.运维专线 2.现象: 到网关和防火墙上.下联口不丢包,到网联通和运维专线方向丢包4%左右,电信和FQ方向不丢包 3.分析 采用从 ...

  6. 51nod1305

    可以暴力,但这里学习了一个新思路,就是把原式进行分解会得到[1/a[i]+1/a[j]],因为向下取整,我们可以发现,1作用于1结果为2,1作用于除了1之外的数结果为1,2作用于2结果为1,所以我们只 ...

  7. 关于loadtxt编码问题的解决方法

    I am trying to load data with numpy.loadtxt... The file im trying to read is using cp1252 coding. Is ...

  8. Python实例浅谈之三Python与C/C++相互调用

    一.问题 Python模块和C/C++的动态库间相互调用在实际的应用中会有所涉及,在此作一总结. 二.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不经过 ...

  9. ABP框架系列之二十:(Dependency-Injection-依赖注入)

    What is Dependency Injection If you already know Dependency Injection concept, Constructor and Prope ...

  10. 秒杀系统-service

    在Dao层我们只完成了针对表的相关操作,包括写了接口方法和映射文件中的sql语句,并没有编写逻辑的代码,例如对多个Dao层方法的拼接,当我们用户成功秒杀商品时我们需要进行商品的减库存操作(调用Seck ...