【Construct Binary Tree from Inorder and Postorder Traversal】cpp
题目:
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 a binary tree node.
* 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) {
if ( inorder.size()== || postorder.size()== ) return NULL;
return Solution::buildTreeIP(inorder, , inorder.size()-, postorder, , postorder.size()-);
}
static TreeNode* buildTreeIP(
vector<int>& inorder,
int bI, int eI,
vector<int>& postorder,
int bP, int eP )
{
if ( bI > eI ) return NULL;
TreeNode *root = new TreeNode(postorder[eP]);
int rootPosInorder = bI;
for ( int i = bI; i <= eI; ++i )
{
if ( inorder[i]==root->val ) { rootPosInorder=i; break; }
}
int leftSize = rootPosInorder - bI;
int rightSize = eI - rootPosInorder;
root->left = Solution::buildTreeIP(inorder, bI, rootPosInorder-, postorder, bP, bP+leftSize-);
root->right = Solution::buildTreeIP(inorder, rootPosInorder+, eI, postorder, eP-rightSize, eP-);
return root;
}
};
tips:
思路跟Preorder & Inorder一样。
这里要注意:
1. 算左子树和右子树长度时,要在inorder里面算
2. 左子树和右子树长度可能一样,也可能不一样;因此在计算root->left和root->right的时候,要注意如何切vector下标(之前一直当成左右树长度一样,debug了一段时间才AC)
==============================================
第二次过这道题,沿用了之前construct binary tree的思路,代码一次AC。
/**
* Definition for a binary tree node.
* 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)
{
return Solution::build(inorder, , inorder.size()-, postorder, , postorder.size()-);
}
TreeNode* build(
vector<int>& inorder, int bi, int ei,
vector<int>& postorder, int bp, int ep)
{
if ( bi>ei || bp>ep) return NULL;
TreeNode* root = new TreeNode(postorder[ep]);
int right_range = ei - Solution::findPos(inorder, bi, ei, postorder[ep]);
int left_range = ei - bi - right_range;
root->left = Solution::build(inorder, bi, ei-right_range-, postorder, bp, ep-right_range-);
root->right = Solution::build(inorder, bi+left_range+ , ei, postorder, bp+left_range, ep-);
return root;
}
int findPos(vector<int>& order, int begin, int end, int val)
{
for ( int i=begin; i<=end; ++i ) if (order[i]==val) return i;
}
};
【Construct Binary Tree from Inorder and Postorder Traversal】cpp的更多相关文章
- 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】
我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列, ======O=========== 后序遍 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: 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 - LeetCode Construct Binary ...
- 【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: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 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 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 ...
随机推荐
- Div 不换行、垂直居中等样式
1. Div内文本过长不换行 1.1 文本不换行 超出部分显示"..." .style1 { float:left; white-space:nowrap; text-overfl ...
- (一)、NodeJS (转载)
NodeJS基础 ------ 转载自阿里巴巴 什么是NodeJS JS是脚本语言,脚本语言都需要一个解 ...
- 请注意designer中的NAME field
If you are trying to set the value of a field through PeopleCode and that field happens to be NAME t ...
- C# 获取web.config配置文件内容
1.web.config提供对客户端应用程序配置文件的访问. 其有两个属性1.ConnectionStrings 获取当前应用程序默认配置的 ConnectionStringsSection 数据. ...
- 错记-checkbox radio
很多时候我想会用到浏览器默认的单选按钮或者复选框,比如说偷懒的时候或者心情不好的时候╮(╯﹏╰)╭, 在html结构里我想实现点击文字旁边的单选按钮就跟着选中或反之,像这样:
- ASP.NET的错误处理机制之一(概念)
对Web应用程序来说,发生不可预知的错误和异常在所难免,我们必须为Web程序提供错误处理机制.当错误发生时,我们必须做好两件事情:一是将错误信息记录日志,发邮件通知网站维护人员,方便技术人员对错误进行 ...
- HTML5开发规范
1.总体规范——采用html5的结构标签进行页面布局,注意结构的语义化,并注意页面大纲的层级结构.使用css3.0进行样式的设计. a.网页大纲查询网址http://gsnedders.html5.o ...
- App.config的学习笔记
昨天基本弄清config的使用之后,再看WP的API,晕了.结果WP不支持system.configuration命名空间,这意味着想在WP上用App.config不大可能了. WP具体支持API请查 ...
- hdu 1008
题目意思是:给你N个数字 每个数字表示多少层楼 现在要你从0层楼开始坐电梯 一次按顺序走过这些楼层 规则是 上楼6秒 ,下楼4秒,每次到达一个楼层停5秒..... 思路:模拟 代码如下:(要注意 ...
- HDU1006
Problem Description The three hands of the clock are rotating every second and meeting each other ma ...