LeetCode 106. 从中序与后序遍历序列构造二叉树(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
解题思路
利用回溯的思想,分别记录生成树时中序遍历和后序遍历对应的段首、段尾,每次构造树时首先构造根节点为后序遍历的尾节点,接着在中序遍历序列中找到根的位置,然后根左对应左子树,根右对应右子树,对应到后序遍历序列中分隔成两段,递归构造左子树和右子树。
代码
/**
* Definition for a binary tree node.
* 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 build(inorder, postorder, , inorder.size() - , , postorder.size() - );
}
TreeNode* build(vector<int> inorder, vector<int> postorder, int iLeft, int iRight, int pLeft, int pRight){
if(pLeft > pRight) return NULL;
TreeNode* root = new TreeNode(postorder[pRight]);
int idx = iLeft;
while(inorder[idx] != postorder[pRight]) idx++;
root->left = build(inorder, postorder, iLeft, idx - , pLeft, pLeft + idx - iLeft - );
root->right = build(inorder, postorder, idx + , iRight, pLeft + idx - iLeft, pRight - );
return root;
}
};
LeetCode 106. 从中序与后序遍历序列构造二叉树(Construct Binary Tree from Inorder and Postorder Traversal)的更多相关文章
- [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | 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)
目录 题目描述: 示例: 解法: 题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 示例: 给出 中序遍历 inorder = [9,3,15,20,7] ...
- [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【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 ...
- 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 ...
- 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] 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 ...
随机推荐
- form表单中的enctype 属性以及post请求里Content-Type方式
对于form表单中的enctype 属性之前理解的一般,就知道是类似于一种编码形式.后来公司做一个form表单提交数据的时候,重点是这个form表单里有文件上传,而我又要用vue来模拟form表单提交 ...
- Java设计模式只好
有时,一些学生私下问我:如何学习前端问题.这里有一个统一的回复,下次我遇到这个问题,同学们会直接给你发这篇文章的链接地址. “如何学习前端”应该因人而异,其他人的方法可能不适合自己.让我们谈谈我的学习 ...
- ie8 下的半透明 background:rgba 与opacity失效 兼容办法
fliter: filter: progid:DXImageTransform.Microsoft.Alpha(opacity=70);
- go语言入门(5)工程管理
在工程中不会简单到只有一个源代码文件,且源文件之间会有相互的依赖关系,早期Go语言使用makefile作为工程管理的临时方案,后来的Go命令行工具的革命性之处在于彻底消除了工程文件的概念,完全用目录结 ...
- 经典i++和++i问题(附带运算符优先级问题)
转自 https://blog.csdn.net/mustard1020/article/details/79617865 1.i++和++i的区别 (1)i++简单来说就是先用i的值来参加表 ...
- arm交叉编译sudo-1.8.6p7
1.交叉编译 # tar -xvf sudo-1.8.6p7.tar.gz # cd sudo-1.8.6p7/ # mkdir build # ./configure --prefix=/home/ ...
- 最贵的AMD 7nm显卡来了!这设计 够狂野
ROG STRIX系列配备三个风扇(支持智能启停).多条热管.金属背板,厚度达2.5个插槽位,另有两个8针辅助供电,因此最大供电能力375W,必然会预先大幅超频,而且应该仍有手动超频空间,当然也少不了 ...
- unix/linux静态库简介
一.创建静态库: 1.写源程序 2.编译源程序,生成.o文件 3.使用ar打包工具生成静态库 ar -r libxxx.a xxx1.o xxx2.o.../*.o(通配符方式) 4.提供头文件,方便 ...
- 1.Nginx安装
1.Nginx安装配置 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发的高性能 Web和 反向代理服务器,也是一个 IMAP/POP3/ ...
- html页面嵌入php代码不显示内容
新建一个html文件,内容如下: <html> <head> <title>Example</title> </head> <body ...