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 ...
随机推荐
- 前端学习之路——scss篇
一.什么是SASS SASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护. 二.安装和使用 Sass依赖于ruby环境,所以装sass之前先 ...
- 不实例化一个 class 的时候使用它的property
class A: @property def name(self): " print(A.name) # <property object at 0x10d54cf98> cla ...
- HDU-2222 Keywords Search 字符串问题 AC自动机
题目链接:https://cn.vjudge.net/problem/HDU-2222 题意 给一些关键词,和一个待查询的字符串 问这个字符串里包含多少种关键词 思路 AC自动机模版题咯 注意一般情况 ...
- HDU Integer's Power(容斥原理)
题意 求[l,r]的最大指数和(1<=l,r<=10^18) 最大指数和(如64=8^2=4^3=2^6,所以64的最大指数和是6) 题解 很明显我们可以先求出[1,n]的最大指数和,然后 ...
- Adobe Flex迷你教程 —Flex4全屏显示
应用场景 1.播放器 我们经常看视频的时候,需要全屏显示,(在flex中这个视频初始化的时候是嵌入到html的iframe中). 2.监控 如下图所示,大多时候我们的监控用的是flex,而树形菜单和标 ...
- SQL的四种语言:DDL、DML、DCL、TCL
1. DDL(Data Definition Language) 数据库定义语言statements are used to define the database structure or sche ...
- bzoj1801: [Ahoi2009]chess 中国象棋(DP)
1801: [Ahoi2009]chess 中国象棋 题目:传送门 题解: 表示自己的DP菜的抠脚 %题解... 定义f[i][j][k]表示前i行 仅有一个棋子的有j列 有两个棋子的有k个 的方案数 ...
- thinkPHP的模板是做什么用的
thinkPHP的模板是做什么用的 问题 为什么PHP中ThinkPHP有做类似模板引擎的东西?smarty也是?这些到底有何用? 我是真没发现它们的用处在哪里?分离了前端和PHP的依赖?HTML文件 ...
- 当fastJson邂逅大写字段时
在项目中遇到了一件令人头疼的事.使用fastJson反序列化时下面的Json时,得到对象属性总为null(如下图),可能细心的朋友一看就知道问题出在哪里,没错!问题就出在返回的字段首字母给大写了.fa ...
- 前端验证银行卡(Luhn校验算法)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...