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

 //先序和中序
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. T4模板在项目中的使用

    建立T4模板方法:右键添加新项->文本模板 使用T4模板生成Dal层代码如下: <#@ template language="C#" debug="false ...

  2. 软件工程(QLGY2015)第一次作业小结(含成绩)

    相关博文目录: 第一次作业点评 第二次作业点评 第三次作业点评 Github项目提交 github的代码提交,大部分人都只是提交了单个文件,存在几个问题 请提交完整的项目文件到github 问题:为什 ...

  3. javascript 漏洞

    1.javascript语言中,每一个对象都有一个对应的原型对象,称为prototype对象.  继承是基于原型的! 2.prototype对象的作用,就是定义所有实例对象共享的属性和方法! 3.“原 ...

  4. nginx + Lua 实现自定义WAF

    文章摘自:https://github.com/unixhot/waf wget git@github.com:unixhot/waf.git

  5. js简单的工厂模式

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...

  6. 细菌觅食算法-python实现

    BFOIndividual.py import numpy as np import ObjFunction class BFOIndividual: ''' individual of bateri ...

  7. BZOJ-2875 随机数生成器 矩阵乘法快速幂+快速乘

    题目没给全,吃X了... 2875: [Noi2012]随机数生成器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 1479 Solved: 829 ...

  8. Ext.Net学习笔记24:在ASP.NET MVC中使用Ext.Net

    在前面的笔记中已经介绍了如何在ASP.NET WebForm中使用Ext.Net,由于这个系列一直在WebForm中使用,所以并没有涉及到ASP.NET MVC中的用法. 如果你要在ASP.NET M ...

  9. linux在线学习

    https://www.shiyanlou.com/courses/running/291#note

  10. js中数组以及for循环的使用

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...