leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal
这道题是为数不多的感觉在读本科的时候见过的问题。
人工构造的过程是如何呢。兴许遍历最后一个节点一定是整棵树的根节点。从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右側的递归构造右子树。元素本身分配空间,作为根节点。
于set和map容器不同的是。vector容器不含find的成员函数。应该用stl的库函数,好在返回的也是迭代器,而vector的迭代器之间是能够做减法的。偏移量非常方便的得到。
TreeNode *buildRec(vector<int> &inorder, int si, vector<int> &postorder, int so, int len){
if(len <= 0) return NULL;
TreeNode *root = new TreeNode(postorder[so]);
int index = find(inorder.begin(), inorder.end(), postorder[so]) - inorder.begin();
int newlen = index - si;
root->left = buildRec(inorder, si, postorder, so-len+newlen, newlen);
root->right = buildRec(inorder, index+1, postorder, so-1, len-newlen-1);
return root;
}
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return buildRec(inorder, 0, postorder, inorder.size()-1, inorder.size());
}
};
leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- 【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 OJ 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 OJ: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 ...
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...
- leetcode第一刷_Construct Binary Tree from Preorder and Inorder Traversal
构造方式跟中序与后序全然一样,并且一般都习惯正着来,所以更简单. 代码是之前写的,没实用库函数,不应该. TreeNode *buildIt(vector<int> &preord ...
- 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 Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
随机推荐
- fiddler--手机https
1.访问fiddler的主讲加端口,比如我的是:http://192.168.1.103:8080 在点击fiddlerroot certificate 下载安装证书即可
- JConsole是什么
从Java 5开始 引入了 JConsole.JConsole 是一个内置 Java 性能分析器,可以从命令行或在 GUI shell 中运行.您可以轻松地使用 JConsole(或者,它更高端的 “ ...
- Qt双缓冲机制:实现一个简单的绘图工具(纯代码实现)
http://blog.csdn.net/rl529014/article/details/51658350
- Html 小插件7 新闻免费代码
http://rss.cnfol.com/freecode/ 定制方法图:
- Android Studio配置(build优化和as优化)
首先是用户目录下的C:\Users\用户名\.gradle\文件下创建gradle.properties文件 输入 org.gradle.daemon=trueorg.gradle.configure ...
- openStack 王者归来之 trivial matters
<一,openStack img 制作> tips:制作大部分cloud platforms img准备工作. <1,> http://www.pubyun.com/blog/ ...
- SQL Server数据转换【包括Geometry类型】的技巧总结
1. 字段的组合: update new_master_location set tmp_street_unique=street+'_'+city+'_'+state+'_'+zip+'_'+con ...
- 纯css加伪类实现关闭提示创窗口
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- JavaScript之将JS代码放在什么位置最合适
1.放到<head></head>标签里面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...
- sql 查询所有数据库、表名、表字段总结
ms sql server 1.查询所有表select [id], [name] from [sysobjects] where [type] = 'u' order by [name]2.查询所有数 ...