根据先序和中序构造二叉树、根据中序和后序构造二叉树,基础题,采用递归的方式解决,两题的方法类似。需要注意的是迭代器的用法。

 //先序和中序
TreeNode *buildTree(vector<int>& preorder, vector<int>& inorder)
{
return buildTree(begin(preorder), end(preorder), begin(inorder), begin(inorder));
}
template<typename InputIterator>
TreeNode *buildTree(InputIterator pre_first, InputIterator pre_last,
InputIterator in_first, InputIterator in_last)
{
if (pre_first == pre_last)return nullptr;
if (in_first == in_last)return nullptr;
//先序第一个为根结点
auto root = new TreeNode(*pre_first);
//查找根结点在中序的位置,返回的是迭代器
auto intRootPos = find(in_first, in_last, *pre_first);
//得到根结点的左半部分
auto leftSize = distance(in_first, intRootPos);
//递归构造,注意去掉根结点
root->left = buildTree(next(pre_first), next(pre_first, leftSize),
in_first, next(in_first, leftSize));
root->right = buildTree(next(pre_first, leftSize), pre_last, next(intRootPos), in_last); return root;
}
//中序和后序
TreeNode *buildTree1(vector<int>& inorder, vector<int>& postorder)
{
return buildTree1(begin(inorder), end(inorder), begin(postorder), begin(postorder));
}
template<typename InputIterator>
TreeNode *buildTree1(InputIterator in_first, InputIterator in_last,
InputIterator post_first, InputIterator post_last)
{
if (in_first == in_last)return nullptr;
if (post_first == post_last)return nullptr;
//后序最后一个为根结点
const auto val = *prev(post_last)
auto root = new TreeNode(val);
//查找根结点在中序的位置,返回的是迭代器
auto intRootPos = find(in_first, in_last, val);
//得到根结点的左半部分
auto leftSize = distance(in_first, intRootPos);
//递归构造,注意去掉根结点
root->left = buildTree(in_first, intRootPos,
post_first, next(post_first, leftSize));
root->right = buildTree(next(intRootPos), in_last,
next(post_first,leftSize), prev(post_last)); return root;
}

Leetcode 之Construct Binary Tree(52)的更多相关文章

  1. (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

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

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

  3. [LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal

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

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

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

  6. LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal

    原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...

  7. [LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  8. [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

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

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

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

随机推荐

  1. JS的十大经典算法排序

    引子 有句话怎么说来着: 雷锋推倒雷峰塔,Java implements JavaScript. 当年,想凭借抱Java大腿火一把而不惜把自己名字给改了的JavaScript(原名LiveScript ...

  2. .net截取两个字符串中间的内容

    做模拟登录时,需要截取html代码中的名字,返回的字符串内容如下 <span class="welcome">您好<span style="font-s ...

  3. 字符串string类型转换成DateTime或DateTime?类型

    常用的Convert.ToDateTime方法 //将含有正确日期格式的string类型转换成DateTime类型 string strDate = "2014-08-01"; D ...

  4. JSON Web Token - 在Web应用间安全地传递信息(zhuan)

    来自 http://blog.leapoahead.com/2015/09/06/understanding-jwt/ JSON Web Token(JWT)是一个非常轻巧的规范.这个规范允许我们使用 ...

  5. git for windows 入门随笔

    引言: Git 是当前最流行的集中化的版本控制程序之一(版本控制是一种记录若干文件内容变化,以便将来查阅特定版本修订情况的系统),Git 只关心文件数据的整体是否发生变化,而大多数其他系统则只关心文件 ...

  6. C/C++语言算法题——替换

    [问题] Description 给定一个有限长度的非负整数序列.一次操作是指从第一个元素开始,依次把数列中的每个数替换为它右边比它小的数的个数.对该数列不断进行这个操作.总有一个时刻该数列将不再发生 ...

  7. Vijos p1770 大内密探 树形DP+计数

    4天终于做出来了,没错我就是这么蒟蒻.教训还是很多的. 建议大家以后编树形DP不要用记忆化搜索,回溯转移状态个人感觉更有条理性. 大神题解传送门 by iwtwiioi 我的题解大家可以看注释&quo ...

  8. Hive 正则匹配函数 regexp_extract

    regexp_extract 语法:    regexp_extract(string subject,  string pattern,  int index) 返回值: string 说明:  将 ...

  9. PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法

    逆向工程中,有时会出现如下错误 ... [Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句 SQLSTATE = 37000 解决方案: 1. ...

  10. jdk版本

    windows: set java_home:查看JDK安装路径 java -version:查看JDK版本 linux: whereis java which java (java执行路径) ech ...