Leetcode106. Construct Binary Tree from Inorder and Postorder Traversal中序后续构造二叉树
根据一棵树的中序遍历与后序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9,15,7,20,3]
返回如下的二叉树:
3 / \ 9 20 / \ 15 7
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder)
{
if(postorder.size() == 0)
return NULL;
if(postorder.size() == 1)
return new TreeNode(postorder[0]);
int iroot = postorder[postorder.size() - 1];
int ipos = 0;
for(int i = 0; i < inorder.size(); i++)
{
if(inorder[i] == iroot)
{
ipos = i;
break;
}
}
TreeNode *root = new TreeNode(iroot);
vector<int> v1(postorder.begin(), postorder.begin() + ipos);
vector<int> v2(inorder.begin(), inorder.begin() + ipos);
vector<int> v3(postorder.begin() + ipos, postorder.end() - 1);
vector<int> v4(inorder.begin() + ipos + 1, inorder.end());
root ->left = buildTree(v2, v1);
root ->right = buildTree(v4, v3);
return root;
}
};
Leetcode106. Construct Binary Tree from Inorder and Postorder Traversal中序后续构造二叉树的更多相关文章
- LeetCode106. Construct Binary Tree from Inorder and Postorder Traversal
题目 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 postorder = [9, ...
- 106 Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树
给定一棵树的中序遍历与后序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出中序遍历 = [9,3,15,20,7]后序遍历 = [9,15,7,20,3]返回如下的二叉树: ...
- 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 ...
随机推荐
- python基础语法(运算符及优先级)
python基础语法(运算符及优先级) python语言支持的运算符类型 算数运算符 假设变量a为10,变量b为21 算数符 描述 实例 + 加-两个对象相加 a+b结果31 - 减-得到一个负数或者 ...
- 3.在vm上安装centos 7
在vm上安装centos 7 1.文件 → 新建虚拟机 3.选择安装Linux系统 4. 虚拟机命名,并选择安装的文件夹 5.选择分配的处理器 6.使用网络地址转换 7.默写选项 9.新建虚拟机 10 ...
- 透视jvm之垃圾回收
JVM是JAVA世界的核心,了解它有助于我们更好调试,调优和开发程序,最近散仙在看JAVA特种兵一书,看完觉得,作者写的内容还是挺不错,大家感兴趣的,也可以购买本温故而知新下. 在JVM中,我们经常提 ...
- css3 html5 手机设备 列表的弹回和加速移动
<style type="text/css"> * { margin: 0; padding: 0; } .min { width: 350px; height: 40 ...
- C++:多线程001
C++ 多线程 创建线程的API函数 HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes,//SD:线程安全相关的属性,常置为N ...
- python spark环境配置
在配置Hadoop之前,应该先做以下配置 1.更改主机名 首先更改主机名,目的是为了方便管理. 输入:hostname 查看本机的名称 使用 hostname 修改当前主 ...
- leetcode-122-买卖股票的最佳时机②
题目描述: 方法一: class Solution: def maxProfit(self, prices: List[int]) -> int: profit = 0 for i in ran ...
- Spring Boot 配置 Security 密码加密
依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...
- 洛谷P2526 【SHOI2001】小狗散步
原题传送门 题目背景 Grant喜欢带着他的小狗Pandog散步.Grant以一定的速度沿着固定路线走,该路线可能自交.Pandog喜欢游览沿途的景点,不过会在给定的N个点和主人相遇.小狗和主人同时从 ...
- 图片转成base64 跨域等安全限制及解决方案
把其他域的图片在canvas中转换为base64时,会遇到跨域安全限制. 目前,唯一可行的方案是,把图片文件以arraybuffer的形式ajax下载下来,然后直接转base4. 但是,这样有个毛病, ...