Data Structure Binary Tree: Construct Tree from given Inorder and Preorder traversals
http://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
using namespace std; struct node {
char data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; void prints(node *root) {
if (!root) return;
prints(root->left);
cout << root->data << " ";
prints(root->right);
} node *buildtree(char in[], char pre[], int beg, int end) {
static int preindex = ;
if (beg > end) return NULL;
node *root = new node(pre[preindex++]);
int index = beg;
for (int i = beg; i <= end; i++) {
if (in[i] == root->data) {
index = i;
break;
}
}
root->left = buildtree(in, pre, beg, index-);
root->right = buildtree(in, pre, index+, end);
return root;
} int main() {
char in[] = {'D', 'B', 'E', 'A', 'F', 'C'};
char pre[] = {'A', 'B', 'D', 'E', 'C', 'F'};
int len = ;
node *root = buildtree(in, pre, , len-);
prints(root);
return ;
}
Data Structure Binary Tree: Construct Tree from given Inorder and Preorder traversals的更多相关文章
- Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals
http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-travers ...
- Data Structure Binary Search Tree: Find k-th smallest element in BST (Order Statistics in BST)
http://www.geeksforgeeks.org/find-k-th-smallest-element-in-bst-order-statistics-in-bst/ #include < ...
- Data Structure Binary Tree: Lowest Common Ancestor in a Binary Tree
http://www.geeksforgeeks.org/lowest-common-ancestor-binary-tree-set-1/ #include <iostream> #in ...
- Data Structure Binary Tree: Print ancestors of a given binary tree node without recursion
http://www.geeksforgeeks.org/print-ancestors-of-a-given-binary-tree-node-without-recursion/ #include ...
- Data Structure Binary Tree: Convert a given Binary Tree to Doubly Linked List
http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ #include &l ...
- Data Structure Binary Tree: Iterative Postorder Traversal
http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/ #include <iostream> #i ...
- Data Structure Binary Tree: Largest Independent Set Problem
http://www.geeksforgeeks.org/largest-independent-set-problem/ #include <iostream> #include < ...
- Data Structure Binary Tree: Morris traversal for Preorder
http://www.geeksforgeeks.org/morris-traversal-for-preorder/ #include <iostream> #include <v ...
- Data Structure Binary Tree: Boundary Traversal of binary tree
http://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/ #include <iostream> #include & ...
随机推荐
- spring aop中的propagation(传播属性)的7种配置的意思
1.前言. 在声明式的事务处理中,要配置一个切面,即一组方法,如 <tx:advice id="txAdvice" transaction-manager="t ...
- windows远程桌面如果超出最大连接数, 使用命令行mstsc /console登录即可
远程桌面如果超出最大连接数, 使用命令行mstsc /console登录即可. (也可以用 mstsc /admin) 可以在运行里使用mstsc /console /v:IP:远程端口即可强制登录; ...
- 程序员必备SQL语句优化技巧
1.任何地方都不要使用 select * from t ,用具体的字段列表代替"*",不要返回用不到的任何字段. 2.尽量使用数字型字段,字符型会降低查询和连接的性能,并会增加存储 ...
- MYSQL总结之sql语句大全
一.基础1.说明:创建数据库 CREATE DATABASE database-name .说明:删除数据库 drop database dbname .说明:备份sql server --- 创建 ...
- PHPthinking为全部PHP爱好者指路
1.新手学习路线:个人觉得.自己手上应该有本參考书.像细说PHP,然后看书学习,穿插网上的一些免费视频教程(随便百度一下即可,之后我尽量整理一些给大家).当然假设自控能力差的同学.也能够參加一些培训机 ...
- [译]NeHe教程 - 你的第一个多边形
原文: Your First Polygon 在第一节中我讲解了如何创建OpenGL窗体.本节我会讲解如何创建三角形和四边形.我们会用GL_TRIANGLES来创建三角形,用GL_GUADS创建四边形 ...
- leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法
Pow(x, n) Implement pow(x, n). 思路:题目不算难.可是须要考虑的情况比較多. 详细代码例如以下: public class Solution { public doubl ...
- 关于quartusII 错误 Error: Current license file does not support the EP*** device 错误原因总结
关于quartusII 错误 Error: Current license file does not support the EP*** device 错误原因总结 第一,有的人用了破解文件lice ...
- mysql单表导入数据,全量备份导入单表
(1)“导出”表 导出表是在备份的prepare阶段进行的,因此,一旦完全备份完成,就可以在prepare过程中通过--export选项将某表导出了: innobackupex --apply-log ...
- 在Ubuntu下利用Eclipse调试FFmpeg
[编辑]第一步:准备编译环境 .tar.bz2 -2245/ ./configure -linux-i586.tar.gz 解压后得到jre1.7.0_17目录 最后通过java -version检查 ...