Q4.3
2014-03-12 11:09
232 查看
#include <iostream> using namespace std; typedef struct Node { int data; struct Node* lchild; struct Node* rchild; }Node, *pNode; bool used[100]; void minHeightTree(pNode head, int* src, int start, int end) { if (start == end) { return; } int mid = (start + end) / 2; pNode p = new Node; if (used[mid] && mid < end && used[mid+1] < end) { mid++; } if (used[mid]) { return; } p->data = src[mid]; p->lchild = p->rchild = NULL; used[mid] = true; if (!head->lchild && p->data < head->data) { head->lchild = p; } else if(!head->rchild && p->data > head->data) { head->rchild = p; } minHeightTree(p, src, start, mid); minHeightTree(p, src, mid, end); } void printTree(pNode head) { if (!head) { return; } cout<<head->data<<endl; if (head->lchild) { cout<<head->data<<"lchild:"; printTree(head->lchild); } if (head->rchild) { cout<<head->data<<"rchild:"; printTree(head->rchild); } } int main(void) { int src[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int len = sizeof(src) / sizeof(int); pNode head = new Node; head->lchild = head->rchild = NULL; memset(used, false, sizeof(used)); minHeightTree(head, src, 0, len - 1); printTree(head); return 0; }
相关文章推荐
- jmail4.3发送邮件带附件方法
- Inside the C++ Object Model学习笔记[Chap4.3-4.4]
- Number Theory 4.3 Prime Examples
- FastReport 4.3 最新补丁
- FastReport 4.3 最新补丁
- 4.3 自动将属性成员分组
- 第四章 4.3 分词原理 Chinese Word Parsing Algorithm
- [实况转播]从KDE 4.2升级到4.3指南@gentoo(跨slot)
- redhat9下安装qt-4.3.x及qtopia-4.3.x 让你的QT飞起来
- jbpm4.3自由流程(动态路由)的实现
- 读书笔记之编程之美 - 4.3 买票找零 (太难了!)
- a bug of ez4.3
- 4.3小记
- 【贺】S5pv210 工控板 +4.3 LCD启动成功!
- iOS 4.3 升级到 iOS 5.0 API Differences(不同)
- 08-27~29 HDU1010 USACO4.3~4.4 搜索剪枝,模拟,拓扑,网络流
- xcode4.2 IOS5 编译低于sdk4.3的程序的办法(转)
- 在 Xcode 4.3 中使用静态库的方法。
- EF4.3 到底能不能用?
- 2012C++程序设计实验报告【4.3】