http://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <stack>
  6. #include <string>
  7. #include <fstream>
  8. using namespace std;
  9.  
  10. struct node {
  11. char data;
  12. struct node *left, *right;
  13. node() : data(), left(NULL), right(NULL) { }
  14. node(int d) : data(d), left(NULL), right(NULL) { }
  15. };
  16.  
  17. void prints(node *root) {
  18. if (!root) return;
  19. prints(root->left);
  20. cout << root->data << " ";
  21. prints(root->right);
  22. }
  23.  
  24. node *buildtree(char in[], char pre[], int beg, int end) {
  25. static int preindex = ;
  26. if (beg > end) return NULL;
  27. node *root = new node(pre[preindex++]);
  28. int index = beg;
  29. for (int i = beg; i <= end; i++) {
  30. if (in[i] == root->data) {
  31. index = i;
  32. break;
  33. }
  34. }
  35. root->left = buildtree(in, pre, beg, index-);
  36. root->right = buildtree(in, pre, index+, end);
  37. return root;
  38. }
  39.  
  40. int main() {
  41. char in[] = {'D', 'B', 'E', 'A', 'F', 'C'};
  42. char pre[] = {'A', 'B', 'D', 'E', 'C', 'F'};
  43. int len = ;
  44. node *root = buildtree(in, pre, , len-);
  45. prints(root);
  46. return ;
  47. }

Data Structure Binary Tree: Construct Tree from given Inorder and Preorder traversals的更多相关文章

  1. 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 ...

  2. 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 < ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Data Structure Binary Tree: Iterative Postorder Traversal

    http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/ #include <iostream> #i ...

  7. Data Structure Binary Tree: Largest Independent Set Problem

    http://www.geeksforgeeks.org/largest-independent-set-problem/ #include <iostream> #include < ...

  8. Data Structure Binary Tree: Morris traversal for Preorder

    http://www.geeksforgeeks.org/morris-traversal-for-preorder/ #include <iostream> #include <v ...

  9. Data Structure Binary Tree: Boundary Traversal of binary tree

    http://www.geeksforgeeks.org/boundary-traversal-of-binary-tree/ #include <iostream> #include & ...

随机推荐

  1. UIScrollView翻书效果

    代码地址如下:http://www.demodashi.com/demo/12695.html 前言:看到凤凰新闻 头条栏目的编辑推荐新闻是这个效果,觉得不错,就想着实现一下,以下就是我的实现过程. ...

  2. Servlet的API(一)

    Servlet的API有很多,这里只谈谈两个Servlet对象:ServletConfig对象和ServletContext对象. 1. ServletConfig对象 在Servlet的配置文件中, ...

  3. Android中ProgressBar

    ProgressBar提供如下方法来操作进度: setProgress(int):设置进度完成的百分比; incrementProgressBy(int):设置进度条的进度的增加或减少,具体取决于参数 ...

  4. ListView知识点汇总(9.2)

    1 最为基础的listview: http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html http://blog.csdn.net/h ...

  5. Ubuntu下git使用

    sudo apt-get install git //安装git git config --global user.name "github 用户名" git config --g ...

  6. Java编程手冊-Collection框架(上)

    该文章所讲内容基本涵盖了Collection里面的全部东西,尽管基于jdk 1.5的.可是思路非常清晰 1.引言 1.1 Collection框架的介绍 尽管我们能够使用数组去存储具有同样类型的元素集 ...

  7. java之数字彩虹雨

    © 版权声明:本文为博主原创文章,转载请注明出处 数字彩虹雨: 从上至下,随机出现一串字符串,以不同的速度运行到底部:类似于黑客帝国里面的场景 GitHub:https://github.com/Ta ...

  8. Spring4整合Hibernate5时不能自动生成表结构

    © 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: Spring4整合Hibernate5时,不再使用hibernate.cfg.xml,将其内容整合到Spring配置文件中,启动后不能 ...

  9. LINUX find 实用

    查找文件find ./ -type f 查找目录find ./ -type d 查找名字为test的文件或目录find ./ -name test 查找名字符合正则表达式的文件,注意前面的‘.*’(查 ...

  10. web service json 数组解析

     boolean workexpMark = true;     // 美发师工作经历json数组解析     org.json.JSONObject jsonObject = new org.j ...