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 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:
- typedef vector<int>::iterator Iter;
- TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
- // IMPORTANT: Please reset any member data you declared, as
- // the same Solution instance will be reused for each test case.
- return buildTreeRecur(inorder.begin(), inorder.end(), postorder.begin(), postorder.end());
- }
- TreeNode *buildTreeRecur(Iter istart, Iter iend, Iter pstart, Iter pend)
- {
- if(istart == iend)return NULL;
- int rootval = *(pend-);
- Iter iterroot = find(istart, iend, rootval);
- TreeNode *res = new TreeNode(rootval);
- res->left = buildTreeRecur(istart, iterroot, pstart, pstart+(iterroot-istart));
- res->right = buildTreeRecur(iterroot+, iend, pstart+(iterroot-istart), pend-);
return res;- }
- };
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 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:
- typedef vector<int>::iterator Iter;
- TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
- // IMPORTANT: Please reset any member data you declared, as
- // the same Solution instance will be reused for each test case.
- return buildTreeRecur(inorder.begin(), inorder.end(), preorder.begin(), preorder.end());
- }
- TreeNode *buildTreeRecur(Iter istart, Iter iend, Iter pstart, Iter pend)
- {
- if(istart == iend)return NULL;
- int rootval = *pstart;
- Iter iterroot = find(istart, iend, rootval);
- TreeNode *res = new TreeNode(rootval);
- res->left = buildTreeRecur(istart, iterroot, pstart+, pstart++(iterroot-istart));
- res->right = buildTreeRecur(iterroot+, iend, pstart++(iterroot-istart), pend);
- return res;
- }
- };
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440560.html
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【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 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
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- leetcode-1006 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-21]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] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- 【LeetCode】105 & 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 ...
- 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 ...
- 【一天一道LeetCode】#106. Construct Binary Tree from Inorder and Postorder Traversall
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
随机推荐
- (第五章)java面向对象之this的作用总结
this关键字总是指向调用该方法的对象. this可以代表任何对象,当this出现在某个方法体中时,它所代表的对象是不确定的,但它的类型是确定的,它所代表的对象只能是当前类的(在那个类中就是那个类), ...
- javascript 的默认对象
一.日期对象 格式 : 日期对象名称=new Date([日期参数]) 日期参数: 1.省略(最常用) 2.英文-参数格式 ...
- ISO9126软件质量模型
ISO9126软件质量模型,是评价软件质量的国际标准.6个特性27个子特性组成. ISO/IEC9126软件质量模型是一种评价软件质量的通用模型,包括3个层次: 1.质量特性 2.质量子特性 3.度量 ...
- CSRF 攻击原理和防御方法
1. CSRF攻击原理 CSRF(Cross site request forgery),即跨站请求伪造.我们知道XSS是跨站脚本攻击,就是在用户的浏览器中执行攻击者的脚本,来获得其cookie等信息 ...
- 读书笔记——Windows环境下32位汇编语言程序设计(3)求复数模的子程序
3.6.1.1中的例子 _Calc proc _dwX,_dwY local @dwResult finit fild _dwX fld st(0) fmul ;i*i fild _dwY fld s ...
- js获取页面传过来的参数
//接收页面传过来的值 //RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i" ...
- 微信公众平台开发教程--方培工作室,PHP语言版本
准备工作 微信公众平台的注册 介绍如何注册一个微信公众账号. 入门教程 微信公众平台开发入门教程 内容:1.申请SAE作为服务器; 2.启用开发模式; 3.微信公众平台PHP SDK; 4.接收发送消 ...
- Mac OS X 设置取消开机自动启动
1. 启动系统设置 (System Preferences) 2. 点以上截图的 用户/组 (User&Groups) 3. 切换到 “登录选项” (Login Items) 可以看到有saf ...
- oracle11G在linux环境下的卸载操作
1.使用SQL*PLUS停止数据库[oracle@OracleTest oracle]$ sqlplus logSQL> connect / as sysdbaSQL> shutdown ...
- C++ enum
为啥需要枚举类型 编程语言中的所有特性都是为了满足某种需求,达到某个目的还出现.不会莫名其妙的出现在那. 枚举可以用来保存一组属性的值.enum的全称是enumeration意思是列举 看着这句话可能 ...