LeetCode_Construct Binary Tree from Inorder and Postorder Traversal
一.题目
Construct Binary Tree from Inorder and Postorder Traversal
Total Accepted: 33418 Total Submissions: 124726My
Submissions
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
二.解题技巧
三.实现代码
#include <iostream>
#include <algorithm>
#include <vector> /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ using std::vector;
using std::find; struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution
{
private:
TreeNode* buildTree(vector<int>::iterator PostBegin, vector<int>::iterator PostEnd,
vector<int>::iterator InBegin, vector<int>::iterator InEnd)
{
if (InBegin == InEnd)
{
return NULL;
} if (PostBegin == PostEnd)
{
return NULL;
} int HeadValue = *(--PostEnd);
TreeNode *HeadNode = new TreeNode(HeadValue); vector<int>::iterator LeftEnd = find(InBegin, InEnd, HeadValue);
if (LeftEnd != InEnd)
{
HeadNode->left = buildTree(PostBegin, PostBegin + (LeftEnd - InBegin),
InBegin, LeftEnd);
} HeadNode->right = buildTree(PostBegin + (LeftEnd - InBegin), PostEnd,
LeftEnd + 1, InEnd); return HeadNode;
}
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
if (inorder.empty())
{
return NULL;
} return buildTree(postorder.begin(), postorder.end(), inorder.begin(),
inorder.end()); }
};
四.体会


LeetCode_Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- 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 ...
- 【题解二连发】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 - LeetCode Construct Binary ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 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: ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
随机推荐
- vue反向代理解决跨域
问题描述 在项目开发的时候,接口联调的时候一般都是同域名下,且不存在跨域的情况下进行接口联调,但是当我们现在使用vue-cli进行项目打包的时候,我们在本地启动服务器后,比如本地开发服务下是 http ...
- 计算机组成原理--64位CPU装载32位操作系统,它的寻址能力还是4GB吗?
借由这个问题,今天我们就把 32 位 CPU.64 位 CPU.32 位操作系统.64 位操作系统之间的区别与联系彻底搞清楚.对于这个问题,博主也是一知半解了好长时间啊~ 基本概念 32位的CPU与6 ...
- Linux 文件系统挂载
文件系统挂载简介 磁盘分区和格式化完成后,磁盘分区要想能够使用,就需要挂载,在挂载某个分区前需要先建立一个挂载点 挂载:将新的文件系统关联至当前根文件系统 卸载:将某文件系统与当前根文件系统的关 ...
- C# 获取本地电脑所有的盘符
话不多说,直接上菜: public List<string> GetRemovableDeviceID() { List<string> deviceIDs = new ...
- Memcached windows安装
Memcached windows安装 如果出现提示: Microsoft Windows [版本 6.3.9600] (c) 2013 Microsoft Corporation.保留所有权利. D ...
- 第九章 TCP和UDP同一时候用复用一个port实现一个回射server
#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include &l ...
- AppCan中标首都机场移动平台项目
近日.正益无线AppCan依托东方航空.吉祥航空.国家电网.中化集团等大客户项目的丰富成功经验,凭借企业移动信息化建设的良好口碑.强大的移动化实施部署经验和高速响应的技术团队,在与多家国内外移动厂商比 ...
- Hadoop入门进阶步步高(二)-文件夹介绍
二.Hadoop文件夹结构 这里重点介绍几个文件夹bin.conf及lib文件夹. 1.$HADOOP_HOME/bin文件夹 文件名 说明 hadoop 用于运行hadoop脚本命令,被hadoop ...
- 131.typename在嵌套类中的作用
#include <iostream> using namespace std; class myit { public: static int num; class itit { }; ...
- 【DNN 系列】 下载安装
1.下载 http://dotnetnuke.codeplex.com/releases/view/119857 2.安装 下载完毕 因为 IIS 7 采用了更安全的 web.config 管理机制, ...