现在的位置: 首页 > 综合 > 正文

Q4.1

2018年04月29日 ⁄ 综合 ⁄ 共 2235字 ⁄ 字号 评论关闭
#include <iostream>
using namespace std;

int maxlen = 0;
int minlen = 65535;

bool isBalance(int maxl, int minl)
{
	return (maxl - minl > 1);
}

int findMin(char* s, int len, int cur, int n)
{
	if (s[cur] == '\0' || cur > len)
	{
		return n - 1;
	}
	else
	{
		int left = findMin(s, len, cur * 2, n + 1);
		int right = findMin(s, len, cur * 2 + 1, n + 1);
		return (left < right)? left : right;
	}
}

int findMax(char* s, int len, int cur, int n)
{
	if (s[cur] == '\0' || cur > len)
	{
		return n - 1;
	}
	else
	{
		int left = findMax(s, len, cur * 2, n + 1);
		int right = findMax(s, len, cur * 2 + 1, n + 1);
		return (left > right)? left : right;
	}
}

// int findMin(char* s, int len, int cur, int n)
// {
// 	if (s[cur] == '\0' || cur > len)
// 	{
// 		return n - 1;
// 	}
// 	else
// 	{
// 		left = findMax(s, len, cur * 2, n + 1);
// 		right = findMax(s, len, cur * 2 + 1, n + 1);
// 		return (left > right)? right : left;
// 	}
// }

int main(void)
{
	int n, len;
	//char src[] = {'\0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', '\0', 'j', '\0', '\0', '\0', 'k', 'l'};
	char src[] = {'\0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', '\0', 'j', '\0', '\0', '\0', 'k'};
	len = sizeof(src)/sizeof(char) - 1;
	cout<<isBalance(findMax(src, len, 1, 0), findMin(src, len, 1, 0))<<endl;

	return 0;
}

// #include <iostream>
// #include <cstring>
// #include <cmath>
// using namespace std;
// 
// const int maxn = 100;
// struct Node{
//     int key;
//     Node *lchild, *rchild, *parent;
// };
// Node *head, *p, node[maxn];
// int cnt;
// 
// void init(){
//     head = p = NULL;
//     memset(node, '\0', sizeof(node));
//     cnt = 0;
// }
// void insert(Node* &head, int x){
//     if(head == NULL){
//         node[cnt].key = x;
//         node[cnt].parent = p;
//         head = &node[cnt++];
//         return;
//     }
//     p = head;
//     if(x < head->key)
//         insert(head->lchild, x);
//     else
//         insert(head->rchild, x);
// }
// int d = 0, num = 0, dep[maxn];
// void getDepth(Node *head){
//     if(head == NULL) return;
//     ++d;
//     getDepth(head->lchild);
//     if(head->lchild == NULL && head->rchild == NULL)
//         dep[num++] = d;
//     getDepth(head->rchild);
//     --d;
// }
// // void getDepth(Node *head){
// //     if(head == NULL) return;
// //     ++d;
// //     
// //     if(head->lchild == NULL && head->rchild == NULL)
// // 	{
// // 		dep[num++] = d;
// // 		d--;
// // 		return;
// // 	}
// // 	getDepth(head->lchild);
// //     getDepth(head->rchild);
// // 	d--;
// // }
// bool isBalance(Node *head){
//     if(head == NULL) return true;
//     getDepth(head);
//     int max = dep[0], min = dep[0];
//     for(int i=0; i<num; ++i){
//         if(dep[i]>max) max = dep[i];
//         if(dep[i]<min) min = dep[i];
//     }
//     if(max-min > 1) return false;
//     else return true;
// }
// int main(){
//     init();
//     int a[] = {
//         5, 3, 8, 1, 4, 7, 10, 2, 6, 9, 11, 12
//     };
//     for(int i=0; i<12; ++i)
//         insert(head, a[i]);
//     cout<<isBalance(head)<<endl;
//     return 0;
// }

【上篇】
【下篇】

抱歉!评论已关闭.