原题地址

思路:

leetcode105题差不多,这道题是给中序和后序,求出二叉树。

解法一:

思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左。

class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
int pos = postorder.size() - 1;
return dfs(inorder, postorder, 0, inorder.size(), pos);
} private:
TreeNode *dfs(vector<int> &inorder, vector<int> &postorder, int beg, int end,
int &pos) {
TreeNode *node = NULL;
if (beg < end) {
int i = 0;
for (i = beg; i < end; ++i)
if (postorder[pos] == inorder[i]) break;
pos--;
node = new TreeNode(inorder[i]);
node->right = dfs(inorder, postorder, i + 1, end, pos);
node->left = dfs(inorder, postorder, beg, i, pos);
}
return node;
}
};
解法二:

解法二是看了别人的题解入手的,和解法一不同思路的是对两个序都用一组beg和end来划分区间。

中序(beg,end)很好划分,假设mid指向当前节点的root值,则分为mid前一半和mid后一半。

后序(pbeg,pend)的划分,是mid - beg的这一段距离。

class Solution
{
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder)
{
return dfs(inorder, postorder, 0, inorder.size(), 0, postorder.size());
} private:
TreeNode *dfs(vector<int> &inorder, vector<int> &postorder, int beg, int end,
int pbeg, int pend)
{
TreeNode *node = NULL;
if (beg < end)
{
int i = 0;
for (i = beg; i < end; ++i)
if (postorder[pend - 1] == inorder[i])
break;
node = new TreeNode(inorder[i]);
node->left = dfs(inorder, postorder, beg, i, pbeg, pbeg + i - beg);
node->right =
dfs(inorder, postorder, i + 1, end, pbeg + i - beg, pend - 1);
}
return node;
}
};

解法一好理解,但是速度慢,用了16ms,击败46%

解法二难点在于理解后序区间划分的依据,用了12ms,击败78%

虽然做了105题,但是做这题还是花了一个半小时。

还是因为不够深入的理解原理,不能总是光看题解就过了。

[leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)的更多相关文章

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

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

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

  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. C#解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 ...

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

    原题地址 二叉树基本操作 [       ]O[              ] [       ][              ]O 代码: TreeNode *restore(vector<i ...

  8. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  9. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

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

随机推荐

  1. Office Add-in Model 为 Outlook Mail Add-in 提供的 JavaScript API 介绍

    本文所讨论的 Mailbox API是指在 Mail Add-in 中可调用的 JavaScript API.开发者可以利用这些API 实现 Add-in 和 Outlook 的交互(数据读取与写入) ...

  2. Using 3D engines with Qt(可以整合到Qt里,不影响)

    A number of popular 3D engines can be integrated with Qt: Contents [hide]  1 Ogre 2 Irrlicht 3 OpenS ...

  3. Ubuntu --- Xshell 连接 VirtualBox下安装的Ubuntu

    1.桥接模式 打开VirtualBox管理器---设置---网络---连接方式选择桥接网卡 2.安装ssh服务 安装: sudo apt-get install openssh-server 启动: ...

  4. SpringBoot从入门到精通十一(SpringBoot文件上传的两种方法)

    前言 在企业级项目开发过程中,上传文件是最常用到的功能.SpringBoot集成了SpringMVC,当然上传文件的方式跟SpringMVC没有什么出入. 本章目标 使用SpringBoot项目完成单 ...

  5. spring 5.x 系列第20篇 ——spring简单邮件、附件邮件、内嵌资源邮件、模板邮件发送 (代码配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 邮件发送配置类为com.heibaiyin ...

  6. spring 5.x 系列第18篇 —— 整合websocket (代码配置方式)

    源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构说明 项目模拟一个简单的群聊功能,为区分不同的聊 ...

  7. 一篇文章概括 Java Date Time 的使用

    本文目的:掌握 Java 中日期和时间常用 API 的使用. 参考:Jakob Jenkov的英文教程Java Date Time Tutorial 和 JavaDoc 概览 Java 8 新增 AP ...

  8. 六种 主流ETL 工具的比较(DataPipeline,Kettle,Talend,Informatica,Datax ,Oracle Goldengate)

    六种 主流ETL 工具的比较(DataPipeline,Kettle,Talend,Informatica,Datax ,Oracle Goldengate) 比较维度\产品 DataPipeline ...

  9. Python将mongodb导出的bson文件转为字典对象

    Python将mongodb导出的bson文件转为字典对象 安装bson包, sudo pip install bson 示例 # 解决编码问题 import sys reload(sys) sys. ...

  10. Python编程菜鸟成长记--A1--03--Python 环境安装(待完成)

    1.重点知识 Windows 上如何安装 Python 3 Linux 上如何安装 Python 3 Mac 上如何安装 Python 3 Windows 上如何安装 Pycharm Mac 上如何安 ...