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 duplicates do not exist in the tree.
- /**
- * Definition for binary 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> &preorder, vector<int> &inorder) {
- if(preorder.size()!=inorder.size()) return NULL;
- return build(preorder,inorder,,preorder.size()-,,inorder.size()-);
- }
- TreeNode *build(vector<int> &preorder, vector<int> &inorder,int s1,int e1,int s2,int e2){
- if(s1>e1||s2>e2) return NULL;
- if(s1==e1) return new TreeNode(preorder[s1]);
- TreeNode *res=new TreeNode(preorder[s1]);
- int tmp=preorder[s1];
- int index=;
- while((s2+index)<=e2){
- if(inorder[s2+index]==tmp)
- break;
- index++;
- }
- res->left=build(preorder,inorder,s1+,s1+index,s2,s2+index-);
- res->right=build(preorder,inorder,s1+index+,e1,s2+index+,e2);
- return res;
- }
- };
construct-binary-tree-from-preorder-and-inorder-traversal——前序和中序求二叉树的更多相关文章
- Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal前序与中序构造二叉树
根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3,15 ...
- LeetCode 105. 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 ...
- 105. Construct Binary Tree from Preorder and Inorder Traversal根据前中序数组恢复出原来的树
[抄题]: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume ...
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 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 Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- [LeetCode] 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 ...
随机推荐
- 数据库导入Exel,输入到浏览器
db.php <?php require dirname(__FILE__)."/dbconfig.php";//引入配置文件 class db{ public $conn= ...
- bootstrap 中dropmenu不起作用
今天在使用bootstrap发现dropmenu一直不起作用,代码是从官网拷贝过来. 网上查找可以用的页面进行一点点的去除分析,发现竟然是顺序反了导致的. 在使用dropmenu时需要引入jquery ...
- CSS3的writing-mode属性
writing-mode这个CSS属性以前是IE的独有属性,IE5.5浏览器就已经支持了.在很长一段时间里,FireFox, Chrome这些现代浏览器都不支持writing-mode,各大现代浏览器 ...
- 【CCF】地铁修建 改编Dijkstra
[题意] 给定有n个点,m条边的无向图,没有平行边和自环,求从1到n的路径中,最长段的最小值(最短路不再是路径和,而是所有段中的最大值) [AC] #include<iostream> # ...
- 写论文,关于word的技巧
当段落的行间距为固定值的时候,图片会出现显示不全的情况,将行间距先改了再插入图片就没问题了. 从引用那边自动添加目录. ctrl+H打开可以使用通配符,改变字母或者数字的样式等
- poj Pseudoprime numbers 3641
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10903 Accepted: 4 ...
- 移动端H5多平台分享实践--摘抄
作者:大漠 日期:2018-01-20 点击:628 mobile 编辑推荐: 掘金是一个高质量的技术社区,从 CSS 到 Vue.js,性能优化到开源类库,让你不错过前端开发的每一个技术干货. 点击 ...
- 关于toggle的用法
//一个关于鼠标点击 切换场景的代码段 $(document).on('click', '.create-advice-elseparm', function () { $('.advice-else ...
- Yii使用find findAll查找出指定字段的实现方法
Yii使用find findAll查找出指定字段的实现方法,非常实用的技巧,需要的朋友可以参考下. 用过Yii的朋友都知道,采用如下方法: 查看代码 打印 1 modelName::model() ...
- Oracle For 循环添加数据
自己亲自使用的,绝对OK --添加数据declare i number; --用for实现 begin for i in 0 .. 500 loop insert into cust(custsn,t ...