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 ...
随机推荐
- CentOS6下源码安装mysql-5.6.25
1.1.系统环境检查 1)检查系统版本 mkdir -p /server/tools/ cd /server/tools/ cat /etc/redhat-release 2)配置域名解析 vim / ...
- 首次运行tensorflow-gpu 1.0 报错 failed to create cublas handle: CUBLAS_STATUS_NOT_INITIALIZED
发现博客: https://blog.csdn.net/u010752600/article/details/79534910 于是找到解决方法. sudo rm -rf ~/.nv/
- Linux 进程、线程运行在指定CPU核上
/******************************************************************************** * Linux 进程.线程运行在指定 ...
- Swift学习——A Swift Tour 协议和扩展
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/28854395 Protocols an ...
- Chrome Developer Tools 中的 Preview 不显示 HTML 的问题
Chrome Developer Tools 中的 Preview 不显示 HTML 的问题 最近升级到 Chrome V64,发现 Chrome Developer Tools 中的 Preview ...
- Jquery中的has、find、filter方法区别
find方法 find返回的是匹配结果集,作用于后代$(‘li’).find(‘.a’).css(‘background-color’, ‘red’);在li下面查找元素是否有class=a的元素,返 ...
- VirtualBox的vdi映像导入遇到的uuid冲突问题 (转)
virtualbox导入vdi文件时出现下面的问题: 打开hard disk D:\software\GT5.0.0.vdi 失败 Cannot register the hard disk 'D ...
- settimeout()在IE8下参数无效问题解决方法
遇到这个问题,setTimeout(Scroll(),3000); 这种写法在IE8 下 不能够执行,提示参数无效, setTimeout(function(){Scroll()},3000);这种方 ...
- mysql-10临时表、复制表
1.创建临时表 mysql临时表在我们需要保存一些临时数据时非常有用. 临时表只在当前连接可见,当关闭连接时,mysql会自动删除表并释放所有空间. 如果使用客户端创建临时表,只有在管不客户端程序时才 ...
- 给iOS开发新手送点福利,简述UISwitch的属性和用法
UISwitch属性 1. onTintColor 处于on时switch 的颜色 switchImage.onTintColor = [UIColor grayColor]; 2.tintC ...