[LeetCode-20]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.
the idea is similar with the former one [Leetcode-21]
java
public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildIP(inorder, postorder, 0, inorder.length-1, 0, postorder.length-1);
}
public TreeNode buildIP(int[] inorder, int[] postorder, int i_s, int i_e, int p_s, int p_e){
if(p_s>p_e)
return null;
int pivot = postorder[p_e];
int i = i_s;
for(;i<=i_e;i++){
if(inorder[i]==pivot)
break;
}
TreeNode node = new TreeNode(pivot);
int lenRight = i_e-i;
node.left = buildIP(inorder, postorder, i_s, i-1, p_s, p_e-lenRight-1);
node.right = buildIP(inorder, postorder, i+1, i_e, p_e-lenRight, p_e-1);
return node;
}
c++
TreeNode *BuildTreeIP(
vector<int> &inorder,
vector<int> &postorder,
int i_s, int i_e,
int p_s, int p_e){
if(i_s > i_e) return NULL;
int pivot = postorder[p_e];
int i = i_s;
for(;i<i_e;i++){
if(inorder[i] == pivot)
break;
}
int length1 = i-i_s;
int length2 = i_e-i;
TreeNode *node = new TreeNode(pivot);
node->left = BuildTreeIP(inorder, postorder, i_s, i-1, p_s, p_s+length1-1);
node->right = BuildTreeIP(inorder, postorder, i+1, i_e, p_e-length2, p_e-1);
return node; }
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return BuildTreeIP(inorder, postorder, 0, inorder.size()-1, 0, postorder.size()-1);
}
[LeetCode-20]Construct Binary Tree from Inorder and Postorder Traversal的更多相关文章
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 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: ...
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers ...
- (二叉树 递归) 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 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 (用中序和后序树遍历来建立二叉树)
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- C#解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 Traversal 由中序和后序遍历建立二叉树 C++
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 that ...
- leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
代码实现:给定一个中序遍历和后序遍历怎么构造出这颗树!(假定树中没有重复的数字) 因为没有规定是左小右大的树,所以我们随意画一颗数,来进行判断应该是满足题意的. 3 / \ 2 4 /\ / \1 6 ...
随机推荐
- 在Eclipse中修改web项目的名称
在Eclipse中修改web项目的名称 一.误区: 单击要修改名称的项目上右键Refactor->Rename,然后修改成另外一个名称 (光这样是不够的,哪怕你再修改web.xml中的displ ...
- 【漏洞预警】CVE-2017-8464 震网三代漏洞复现
早在6月13日,微软发布补丁修复编号为CVE-2017-8464的漏洞,本地用户或远程攻击者可以利用该漏洞生成特制的快捷方式,并通过可移动设备或者远程共享的方式导致远程代码执行,追溯到以前,NSA就承 ...
- Spring Boot 运作原理
Spring Boot 运作原理 1.Spring Boot 简介 SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了 ...
- CSS3制作ajax loader icon
demo 本文用到的两个CSS3属性:transform.animation 一.HTML <div class="ajax-loading"> <div cla ...
- 我们为什么需要Map-Reduce?
在讨论我们是否真的需要Map-Reduce这一分布式计算技术之前,我们先面对一个问题,这可以为我们讨论这个问题提供一个直观的背景. 问题 我们先从最直接和直观的方式出发,来尝试解决这个问题: 先伪一下 ...
- XStream转换Java对象与XML
1.引入需要的jar包,在pom.xml中配置依赖 <dependency> <groupId>com.thoughtworks.xstream</groupId> ...
- Delphi 调用SQL Server 2008存储过程
1.表结构如下(预算数据明细表): CREATE TABLE [dbo].[BA_FeeDetail]( [ID] [int] IDENTITY(1,1) NOT NULL, [FeeDeptID] ...
- 【转】教你用C#读写、删除、更新excel表格记录
文章出处:http://blog.csdn.net/kuangshazi515/article/details/6585118 如下图所示,编一个程序,鼠标单击窗体视图区(右边)时,获取一对坐标(X, ...
- python笔记28-lxml.etree爬取html内容
前言 本篇继续lxml.etree学习,在线访问接口,通过接口返回的html,解析出想要的text文本内容 环境准备: python 3.6 lxml requets 定位目标 爬取我的博客首页htt ...
- 将properties文件放在Jar包并读取
有时候需要在一个library内部打包一个properties文件,包含一些配置信息,而不能部署在外部. 在maven工程里面,将properties文件放在src/main/resources目录下 ...