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

代码如下:

 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. Windows 10 系统,配置jdk系统环境变量

    1. 下载jdk包,下载路径为:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html, ...

  2. 云笔记项目-Java反射知识学习

    在云笔记项目中,补充了部分反射的知识,反射这一部分基础知识非常重要,前面学习的框架Spring和MyBatis读取xml配置文件创建对象,以及JDBC加载驱动等都用了反射,但只知道有这个东西,具体不知 ...

  3. yii2.0如何优化路由

    比如我的路由是  http://localhost/basic/web/?r=site/index 现在想改成   http://localhost/basic/web/site/index 的形式 ...

  4. java集合: jdk1.8的hashMap原理简单理解

    HashMap的数据结构 HashMap是数组+链表+红黑树(JDK1.8增加了红黑树部分)实现的,他的底层结构是一个数组,而数组的元素是一个单向链表.HashMap默认初始化的是一个长度为16位的数 ...

  5. 清理xcode

    移除 Xcode 运行安装 APP 产生的缓存文件(DerivedData) ~/Library/Developer/Xcode/DerivedData 移除 APP 打包的ipa历史版本(Archi ...

  6. java基础 ------- 多重循环 and break与continue

    -----  什么是多重循环 ----   打印数列 public class ForEx { public static void main(String[] args){ for(int i = ...

  7. 保存一份自己常用的packjson

    这里是一份专门针对react的插件配置, 有: es5的转换器,有ie的promise垫片,有蚂蚁金服的anth,还有用于消息通信的pubsub订阅发布系统,虽然现在不用了.... 用于发请求的axi ...

  8. Python开发——数据类型【数字】

    布尔型 bool型只有两个值:True 或 False 我们将bool值归类为数字,习惯上:1表示true,0表示false 整型 int(整型) 在32位机器上,整数的位数为32位,取值范围为-2* ...

  9. ubuntu中运行java程序

    查找jdk rivsidn@rivsidn:~/demo/java$ sudo apt-cache search jdk default-jdk - Standard Java or Java com ...

  10. Java第三次实验敏捷开发与XP实验

    实验三-1 1.实验要求: 实验三 敏捷开发与XP实践 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成IDEA 参考 http: ...