Construct Binary Tree From Inorder and Preorder/Postorder Traversal
map<int, int> mapIndex;
void mapToIndex(int inorder[], int n)
{
for (int i = ; i < n; i++)
{
mapIndex.insert(map<int, int>::value_type(inorder[i], i);
}
} Node* buildInorderPreorder(int in[], int pre[], int n, int offset)
{
assert(n >= );
if (n == )
return NULL;
int rootVal = pre[];
int i = mapIndex[rootVal] - offset;
Node* root = new Node(rootVal);
root->left = buildInorderPreorder(in, pre+, i, offset);
root->right = buildInorderPreorder(in+i+, pre+i+, n-i-, offset+i+);
return root;
} Node* buildInorderPostorder(int in[], int post[], int n, int offset)
{
assert(n >= );
if (n == )
return NULL;
int rootVal = post[n-];
int i = mapIndex[rootVal] - offset;
Node* root = new Node(rootVal);
root->left = buildInorderPostorder(in, post, i, offset);
root->right = buildINorderPostorder(in+i+, post+i, n-i-, offset+i+);
return root;
}
Construct Binary Tree From Inorder and Preorder/Postorder Traversal的更多相关文章
- 105. Construct Binary Tree from Inorder and preorder Traversal
/* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root ...
- Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- 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 -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 ...
- 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 Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- Spring Boot的启动器Starter详解
Spring Boot的启动器Starter详解 作者:chszs,未经博主允许不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs Spring Boot ...
- Unix/Linux环境C编程入门教程(16) LinuxMint CCPP开发环境搭建
1. Linux Mint由Linux Mint Team团队于2006年开始发行,是一份基于 这个时候linuxmint安装完成,C/C++开发环境也配置完成,希望大家认真实践!
- 【转】stdin, stdout, stderr 以及重定向
详细见: http://my.oschina.net/qihh/blog/55308 stdin是标准输入文件,stdout是标准输出文件,stderr标准出错文件. 程序按如下方式使用这些文件: 标 ...
- 杂记之activity之间的跳转
代码结构图 manifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xml ...
- ActiveMQ使用STOMP协议的一个错误问题:Unexpected ACK received for message-id
使用某些语言环境下的stomp包(比如php python ruby),可能会出现如下问题: Unexpected ACK received for message-id 这一般可能有两个原因. 1. ...
- mysql错误-更改mysql.sock位置
于Mysql在有时会出现mysql.sock定位误差,会造成不连接数据库. mac由当时的误差: 第一个变化my.cnf 位置/etc/my.cnf下一个,如果没有,那么.跟/usr/locate/m ...
- Java环境的配置
JAVA环境: 1.打开我的电脑--属性--高级--环境变量 2.将相应的JDK环境下载到本机,将路径保存到无中文路径中,并将路径复制下来. 3.在环境变量--系统变量,中新建 变量名:JAVA_HO ...
- C#反序列化json字符串时,提示:应为来自命名空间“”的元素“root”。。遇到名称为“”、命名空间为“”的“None”。
反序列化调用接口返回的字符串时,出现:应为来自命名空间“”的元素“root”..遇到名称为“”.命名空间为“”的“None”.,导致反序列化数据失败,这种失败并有时候并不会直接提示反序列化失败(抛异常 ...
- JDK源码学习--String篇(四) 终结篇
StringBuilder和StringBuffer 前面讲到String是不可变的,如果需要可变的字符串将如何使用和操作呢?JAVA提供了连个操作可变字符串的类,StringBuilder和Stri ...
- java 调用jdbc 实现excel和csv的导入和导出
jdbc 的连接 实体类 package com.gpdi.mdata.web.manage.database.data;public class DBQueryData {private Strin ...