leetcode题解: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 duplicates do not exist in the tree.
说明:
1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进
算法思想
- 根据后序遍历的特点,知道后序遍历最后一个节点为根节点,即为A
- 观察中序遍历,A左侧CBEDF为A左子树节点,A后侧HGJI为A右子树节点
- 然后递归的构建A的左子树和后子树
实现:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return creatTree(inorder.begin(),inorder.end(),postorder.begin(),postorder.end());
}
private:
template<typename InputIterator>
TreeNode *creatTree(InputIterator in_beg,InputIterator in_end,InputIterator post_beg,InputIterator post_end)
{
if(in_beg==in_end||post_beg==post_end) return nullptr; //空树
TreeNode *root=new TreeNode(*(post_end-));
auto inRootPos=find(in_beg,in_end,root->val);//中序遍历中找到根节点,返回迭代指针
int leftlen=distance(in_beg,inRootPos);//中序遍历起点指针与找到的根节点指针的距离
root->left=creatTree(in_beg,inRootPos,post_beg,next(post_beg,leftlen));//递归构建左子数
root->right=creatTree(next(inRootPos),in_end,next(post_beg,leftlen),post_end-);//递归构建右子树
return root;
}
};
leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- (二叉树 递归) 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 ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- 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 ...
- 【leetcode】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 ...
- 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 ...
随机推荐
- [bzoj4372] 烁烁的游戏 [动态点分治+线段树+容斥原理]
题面 传送门 思路 观察一下题目,要求的是修改"距离点$u$的距离一定的点权值",那这个就不能用传统的dfs序类算法+线段树维护,因为涉及到向父亲回溯的问题 看到和树上距离相关的东 ...
- h5页面添加背景音乐
[需求]h5页面添加背景音乐,支持微信.QQ.各种APP. [实现思路]1.通过audio标签,设置自动播放,是一种方法,但是此方法只适合微信.QQ,并不兼容我司的APP,需要主动触发下播放事件. 2 ...
- java 竖线分割字符串的问题
java 竖线分割字符串的问题 例1: String[] paraStr = "6010;320100;A".split(";"); System.out.pr ...
- windows 添加自助白名单
由于公司分部用的是动态IP,又需要用到总部的OA系统,OA完全开放对外不安全,所以写了这个工具 项目地址 https://github.com/cainiaoit/Windows-firewall-s ...
- mogilefsdBUG mogilefsd[15624]: crash log: Modification of a read-only value attempted at /usr/local/share/perl5/Sys/Syscall.pm line 227
mogilefsd[15624]: crash log: Modification of a read-only value attempted at /usr/local/share/perl5/S ...
- less与sass的区别点
less与sass: 相同点: 1,两者都作为css扩展技术,也都,基于css的高级预处理语言之上. 2,都有的优点:简化代码,降低维护成本. 3,都必须要避免中文环境,所涉及到的所有目录,标题以及内 ...
- 【linux高级程序设计】(第十五章)UDP网络编程应用 1
UDP网络通信流程 UDP没有connect的过程,故发送数据时需要指明目的地址,不能使用read/write/send/recv. 采用sendto()和recvfrom() ssize_t sen ...
- ef code first commad
PM> enable-migrations 已在项目“EasyWeChat.Data”中启用迁移.若要覆盖现有迁移配置,请使用 -Force 参数. PM> add-migration 位 ...
- Python的程序结构[2] -> 类/Class[5] -> 内建类 bytes 和 bytearray
内建类 bytes 和 bytearray / Built-in Type bytes and bytearray 关于内建类 Python的内建类 bytes 主要有以下几点: class byte ...
- Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) -B C(GCD,最长连续交替序列)
B. Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input ...