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的更多相关文章

  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. 使用PostMan快速生成代码

    Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件.关于PostMan的下载和使用网上有很多相关的博客介绍,本文主要介绍PostMan在进行模拟Http请求后可以根据需要的 ...

  2. js正则表达式的分组提取

  3. 解密和解压浏览器上加密的js文件

    F12 -> 进入Sources -> 找到任意一个加密的js文件,如图 点击最下方的 {} 即可解压

  4. POJ1122_FDNY to the Rescue!(逆向建图+最短路树)

    FDNY to the Rescue! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2368   Accepted: 72 ...

  5. 利用NIO的Selector处理服务器-客户端模型

    package NIOTEST; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocket ...

  6. IIS中使用ASP.NET MVC的经验总结

    在这篇文章中我们学习在不同版本的IIS中使用ASP.NET MVC和URL Routing.我们学习针对IIS7.0.IIS6.0和更早版本的IIS的处理策略. ASP.NET MVC框架依赖于URL ...

  7. nginx 常见参数以及重定向参数配置

    nginx 各参数翻译,作用 $arg_PARAMETER #这个变量包含GET请求中,如果有变量PARAMETER时的值. $args #这个变量等于请求行中(GET请求)的参数,例如foo=123 ...

  8. 安装Hadoop 1.1.2 (三 安装配置Hadoop)

    1 tar -zxvf hadoop-1.1.2.tar.gz 2 在hadoop/conf目录 (1) 编辑 hadoop-env.sh export JAVA_HOME=/usr/java/jdk ...

  9. MoQ(基于.net3.5,c#3.0的mock框架)简单介绍(转)

    https://www.cnblogs.com/nuaalfm/archive/2009/11/25/1610755.html

  10. phpstorm+xdebug, 实现断点调试: xdebug如何配置

    [XDebug] xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug" xdebug.trace_output_dir=" ...