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 & ...
随机推荐
- UIScrollView翻书效果
代码地址如下:http://www.demodashi.com/demo/12695.html 前言:看到凤凰新闻 头条栏目的编辑推荐新闻是这个效果,觉得不错,就想着实现一下,以下就是我的实现过程. ...
- Servlet的API(一)
Servlet的API有很多,这里只谈谈两个Servlet对象:ServletConfig对象和ServletContext对象. 1. ServletConfig对象 在Servlet的配置文件中, ...
- Android中ProgressBar
ProgressBar提供如下方法来操作进度: setProgress(int):设置进度完成的百分比; incrementProgressBy(int):设置进度条的进度的增加或减少,具体取决于参数 ...
- ListView知识点汇总(9.2)
1 最为基础的listview: http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html http://blog.csdn.net/h ...
- Ubuntu下git使用
sudo apt-get install git //安装git git config --global user.name "github 用户名" git config --g ...
- Java编程手冊-Collection框架(上)
该文章所讲内容基本涵盖了Collection里面的全部东西,尽管基于jdk 1.5的.可是思路非常清晰 1.引言 1.1 Collection框架的介绍 尽管我们能够使用数组去存储具有同样类型的元素集 ...
- java之数字彩虹雨
© 版权声明:本文为博主原创文章,转载请注明出处 数字彩虹雨: 从上至下,随机出现一串字符串,以不同的速度运行到底部:类似于黑客帝国里面的场景 GitHub:https://github.com/Ta ...
- Spring4整合Hibernate5时不能自动生成表结构
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: Spring4整合Hibernate5时,不再使用hibernate.cfg.xml,将其内容整合到Spring配置文件中,启动后不能 ...
- LINUX find 实用
查找文件find ./ -type f 查找目录find ./ -type d 查找名字为test的文件或目录find ./ -name test 查找名字符合正则表达式的文件,注意前面的‘.*’(查 ...
- web service json 数组解析
boolean workexpMark = true; // 美发师工作经历json数组解析 org.json.JSONObject jsonObject = new org.j ...