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

 //先序和中序
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. Bata版本冲刺计划及安排

    下一阶段需要改进完善的功能 UI: 1. 界面不够精细,布局不够美观并且尚有BUG没有解决,图形.色彩间不够协调. 2. 理清界面间的跳转逻辑. 搜索: 搜索成功并定位后,不会影响地理标签的显示. 定 ...

  2. 快速安装 GitLab 并汉化

    转载:http://www.jianshu.com/p/7a0d6917e009

  3. iOS运行时 -- Runtime(摘抄自网络)

    运行时(iOS) 一.什么是运行时(Runtime)? 运行时是苹果提供的纯C语言的开发库(运行时是一种非常牛逼.开发中经常用到的底层技术) 二.运行时的作用? 能获得某个类的所有成员变量 能获得某个 ...

  4. Quartz 定时任务管理

    前言 将项目中的所有定时任务都统一管理吧,使用 quartz 定时任务 设计思路 使用 quartz 的相关jar 包,懒得去升级了,我使用的是 quart 1.6 写一个定时任务管理类 用一张数据库 ...

  5. linux更改文件所有者命令chown命令的使用困惑

    [berry@berry:practice] ls -lrt total -rwxrwxrwx berry berry Dec : f1.txt -rwxrwxrwx berry berry Dec ...

  6. BZOJ-3670 动物园 KMP+奇怪的东西

    YveH爷再刷KMP,DCrusher看他刷KMP,跟着两个人一块刷KMP... 3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MB ...

  7. 使用Jayrock开源组件创建参数可为空的接口

    经过上一篇文章对Jayrock开源组件的分析,我发现了一个问题,就是在写接口的时候,可以设置某个参数为空,可以不需要进行参数的传递,具体写法如下: 图上的test参数就是可空类型,只需标识为int?类 ...

  8. c++模板库(简介)

    目 录 STL 简介 ......................................................................................... ...

  9. Advice for applying Machine Learning

    https://jmetzen.github.io/2015-01-29/ml_advice.html Advice for applying Machine Learning This post i ...

  10. ps 倒影制作

    首先打开PS并打开一张素材,这里我选择了山水图片,制作山峰在水中的倒影效果.   然后按下[Crrl+J]复制这个图层,如图:   接着按下[Ctrl+T]或者是[编辑][自由变换],打开[自由变换] ...