106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
root = NULL;
if(inorder.empty()) return root;
root = new TreeNode();
buildSubTree(inorder,postorder,,inorder.size()-,,postorder.size()-,root);
return root; }
void buildSubTree(vector<int> &inorder, vector<int>&postorder, int inStartPos, int inEndPos, int postStartPos, int postEndPos,TreeNode * currentNode)
{
currentNode->val = postorder[postEndPos]; //后序遍历的最后一个节点是根节点 //find root position in inorder vector
int inRootPos;
for(int i = inStartPos; i <= inEndPos; i++)
{
if(inorder[i] == postorder[postEndPos])
{
inRootPos = i;
break;
}
} //right tree: 是中序遍历根节点之后的部分,对应后序遍历根节点前相同长度的部分
int newPostPos = postEndPos - max(inEndPos - inRootPos, );
if(inRootPos<inEndPos)
{
currentNode->right = new TreeNode();
buildSubTree(inorder,postorder,inRootPos+,inEndPos,newPostPos,postEndPos-,currentNode->right);
} //leftTree: 是中序遍历根节点之前的部分,对应后序遍历从头开始相同长度的部分
if(inRootPos>inStartPos)
{
currentNode->left = new TreeNode();
buildSubTree(inorder,postorder,inStartPos,inRootPos-,postStartPos,newPostPos-,currentNode->left);
}
}
private:
TreeNode* root;
};
106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)的更多相关文章
- 【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 ...
- 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: ...
- 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 Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
随机推荐
- EM算法及其应用(一)
EM算法及其应用(一) EM算法及其应用(二): K-means 与 高斯混合模型 EM算法是期望最大化 (Expectation Maximization) 算法的简称,用于含有隐变量的情况下,概率 ...
- Centos安装Chrome浏览器失败解决办法
最近因为项目需要使用到Centos,自己经常使用Chrome,所有的书签以及信息都是同步在Google,所以尝试在Centos上安装Chrome,按照网上的资料都是安装失败,显示缺少资源,不过最终还是 ...
- 网站使用 rel="noopener" 打开外部锚
当您的页面链接至使用 target="_blank" 的另一个页面时,新页面将与您的页面在同一个进程上运行. 如果新页面正在执行开销极大的 JavaScript,您的页面性能可能会 ...
- Nginx 静态资源缓存配置
示例 # Media: images, icons, video, audio, HTC location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|m ...
- try...except语句
try: 执行语句 except 执行语句有异常就执行这一步 else: 执行语句没有异常就执行这一步 finally 不管有没有异常,这一步就要执行
- DesignPattern(四)结构型模式(下)
上篇链接 https://www.cnblogs.com/qixinbo/p/9023764.html 继续介绍最后三种结构型模式 外观模式 外观模式,也称作 ”门面“模式,在系统中,客户端经常需要 ...
- FastAdmin Bootstrap-Table 关于客户端模式(由 计算所有页的的总数引发的思考)
Bootstrap-Table 关于客户端模式(由 计算所有页的的总数引发的思考) 昨天群里有小伙伴询问 Bootstrap-Table 有没有计算所有页的总数. [吐槽]★隔壁老王-杭州 @F4NN ...
- Python download a image (or a file)
http://stackoverflow.com/questions/13137817/how-to-download-image-using-requests import shutil impor ...
- Java-Web 用html和css写一个EasyMall注册界面
要求: html代码: <!DOCTYPE html> <html> <head> <title>EasyMall注册页面</title> ...
- KMP算法解释
给定两个字符串A,B,判断T是否为S的子串(变式:寻找子串B在串A中的位置). 要求一个O(|A|+|B|)的做法. 通常称A为目标串(或主串),B为模式串. 算法过程: 我们假设串A的长度为n,串B ...