#include <iostream>
#include <stack>
#include <queue>
using namespace std; template<typename Type>
struct Node
{
Node* right;
Node* left;
Type data;
Node(Type tp = Type()) :data(tp),right(NULL),left(NULL){}
}; template<typename Type>
class MT
{
public:
MT(const char *s,Type tp)
{
root = NULL;
flags = tp;
Insert(root,s);
} void Insert(Node<Type> *&t,const char *& s )
{
if (*s == flags)
{
t = NULL;
return ;
}
else
{
t = new Node<Type>(*s);
Insert(t->left,++s);
Insert(t->right, ++s);
}
} void A()
{
Node<Type> *t = root;
if (t == NULL)return;
queue<Node<Type> *> st;
st.push(t);
while (st.empty() == false)
{
Node<Type> *t = st.front();
st.pop();
cout << t->data << " ";
if (t->left != NULL)
{
st.push(t->left);
}
if (t->right != NULL)
{
st.push(t->right);
}
}
} int B()
{
return B(root);
}
int C(int x)
{
return C(root,x);
}
void Printf()
{
Printf(root);
}
void D()
{ }
bool IsHere(char ch)
{
return IsHere(root,ch);
} char Parent(char ch1,char ch2)
{
return Parent(root,ch1,ch2);
}
bool IsBanlance()
{
return IsBanlance(root);
}
int GetLengthMax()
{
return GetLengthMax(root);
}
int GetLg()
{
return GetLg(root);
}
private:
//二叉树高度。
int GetLg(Node<Type> *t)
{
if (t == NULL)return 0;
else
{
return GetLg(t->left) > GetLg(t->right) ? GetLg(t->left) + 1 : GetLg(t->right) + 1;
}
} //推断二叉树是不是平衡二叉树。
bool IsBanlance(Node<Type> *t)
{
if (t == NULL)return true;
int len = GetLengthMax(root);
if (len >= 0 && len <= 1)return true;
else return false;
} //求二叉树中最高高度差。
int GetLengthMax(Node<Type>* t)
{
int count = 0;
int mincount = 0x7fffffff;
int maxcount = 0xffffffff;
Getlow(root, count, mincount);
count = 0; GetHigh(root, count, maxcount); return maxcount - mincount;
return 0;
} int Getlow(Node<Type> *t,int count,int &mincount)
{
if (t == NULL)return 0;
if (root->left == NULL || root->right == NULL)
{
mincount = 0;
return 0;
}
if (t == root)
{
count += 1;
}
if ((t->left == NULL && t->right == NULL))
{
mincount = count > mincount ? mincount : count;
return 0;
}
else
{
count++;
Getlow(t->left, count, mincount);
Getlow(t->right, count, mincount);
}
return 0;
}
int GetHigh(Node<Type> *t,int count,int &maxcount)
{
if (t == NULL)return 0;
else
{
count++;
GetHigh(t->left,count,maxcount);
GetHigh(t->right, count, maxcount);
maxcount = count > maxcount ? count : maxcount;
}
return 0;
} //求最低父亲节点(方法一)
/*
char Parent(Node<Type> *t, char ch1, char ch2)
{
if (t == NULL)return '0';
else
{
Parent(t->left, ch1, ch2);
Parent(t->right, ch1, ch2);
//兴许遍历找近期公共父亲节点。
if (IsHere(t, ch1) == 1 && IsHere(t, ch2) == 1)return t->data;
}
}
*/ //求最低父亲节点(方法二)
char Parent(Node<Type> *t,char ch1,char ch2)
{ if (t != NULL)
{
stack<Node<Type> *> st;
st.push(t);
while (st.empty() == false)
{
if (IsHere(t->left, ch1) == 1 && IsHere(t->left,ch2) == 1)
{
st.push(t->left);
t = t->left;
continue;
}
if (IsHere(t->right, ch1) == 1 && IsHere(t->right, ch2) == 1)
{
st.push(t->right);
t = t->right;
continue;
}
break;
}
while (st.empty() == false)
{
Node<Type> *p = st.top();
st.pop();
cout << p->data << endl;
}
}
return 'a';
} //查看树中是否有该字符。
bool IsHere(Node<Type> *t,char ch)
{
if (t == NULL)return false;
if (t->data == ch)return true;
else
{
if (IsHere(t->left, ch))return true;
return IsHere(t->right,ch);
}
} //查看指定层的节点个数。
int C(Node<Type> *t,int x)
{
if (x<1 || t == NULL)return 0;
if (x == 1)
{
return 1;
}
else
{
x--;
return C(t->left, x) + C(t->right, x);
}
} //查看叶子节点的个数。 int B(Node<Type>* t)
{
if (t == NULL)return 0;
if (t->right == NULL && t->left == NULL)
{
return 1;
}
else
{
return B(t->left)+B(t->right);
}
}
//二叉树的前序遍历。 void Printf(Node<Type> *t)
{
if (t == NULL)
{
return;
}
else
{
cout << t->data << " ";
Printf(t->left);
Printf(t->right);
}
} private:
Type flags;
Node<Type> *root;
}; int main()
{
//char s[] = "abcd####e##";
//char s[] = "ab##c##";
//char s[] = "a#b##";
char s[] = "ab##c#d#e#f##";
//char s[] = "abcd####e#f#g#h##"; MT<char> mt(s,'#'); //mt.A();
//cout << mt.C(4) << endl;
//cout <<mt.IsHere('e') << endl;
//cout <<mt.Parent('c','d')<<endl;
//mt.D();
cout << mt.GetLg() << endl;
//cout << mt.GetLengthMax() << endl;
//cout<<mt.IsBanlance()<<endl;
//mt.Printf();
return 0;
}

C++二叉树笔试题的更多相关文章

  1. C/C++ 笔试题

    /////转自http://blog.csdn.net/suxinpingtao51/article/details/8015147#userconsent# 微软亚洲技术中心的面试题!!! 1.进程 ...

  2. 嵌入式Linux C笔试题积累(转)

    http://blog.csdn.net/h_armony/article/details/6764811 1.   嵌入式系统中断服务子程序(ISR) 中断是嵌入式系统中重要的组成部分,这导致了很 ...

  3. C/C++笔试题(很多)

    微软亚洲技术中心的面试题!!! .进程和线程的差别. 线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)调度:线程作为调度和分配的基本单位,进程作为拥有资源的基本单位 (2 ...

  4. 阿里巴巴2013年实习生笔试题B

    阿里巴巴集团2013实习生招聘技术类笔试题(B) 一.单向选择题 1.在常用的网络协议中,___B__是面向连接的.有重传功能的协议. A. IP B. TCP C. UDP D. DXP 2.500 ...

  5. 华为C语言笔试题集合

    ①华为笔试题搜集 1.static有什么用途?(请至少说明两种)     1)在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变.     2) 在模块内(但在函数体外),一个被声明为 ...

  6. java笔试题13-11-21

    中xxx科技公司java笔试题 今天去参加一个公司的面试,去先做了一份笔试题,妈的,太他妈难了(对于我来说,最后做完一个员工说你是不是投错简历了,都是空白,我说我做的大部分都对了..最后面试都没有,就 ...

  7. java各公司笔试题集1

    IBM笔试题 注:IBM笔试题一小时之内完成,题目全部用英文描述,这里用中文表述 一.名词解释 1.Eclipse 2.J2EE 3.EJB 4.Ajax 5.Web service 二.找出以下代码 ...

  8. google2013校园招聘笔试题(全国)

    google2013校园招聘笔试题 1. 单项选择题1.1如果把传输速率定义为单位时间内传送的信息量(以字节计算)多少.关于一下几种典型的数据传输速率:1.使用USB2.0闪存盘,往USB闪存盘上拷贝 ...

  9. google浙大招聘笔试题 师兄只能帮你到这儿了

    google浙大招聘笔试题 一.单选1.80x86中,十进制数-3用16位二进制数表示为?00100002.假定符号-.*.$分别代表减法.乘法和指数运算,且 1)三个运算符优先级顺序是:-最高,*其 ...

随机推荐

  1. .Net Core中使用Quartz.Net Vue开即用的UI管理

    Quartz.NET Quartz.Net 定制UI维护了常用作业添加.删除.修改.停止.启动功能,直接使用cron表达式设置作业执行间隔,有完整的日志记录. Quartz.NET是一个功能齐全的开源 ...

  2. android平台 cocos2d-x 读取相册数据

    现已解决 方案如下: 1.使用 jni 调用 java 方法 启动相册选择框2.使用java将获取的图片保存到本地3.使用Cocos2d-x中 CCImage 读取 JAVA代码如下: //启动图片选 ...

  3. 溢出文本省略号的js实现

    function ellipsis(element) { var limitWidth = element.clientWidth; var temp = element.cloneNode(true ...

  4. [原创]Linux(CentOS)下安装nodejs+express

    网上找了很多步骤,各种问题,自己总结下吧 1.下载 wget --no-check-certificate https://nodejs.org/dist/v6.10.1/node-v6.10.1-l ...

  5. 启动Mysql时,提示error 2002 的解决办法

    故障描述 启动时提示ERROR 2002(HY000) 启动服务时,提示the server quit without updating PID file. 解决方法一: 1.由于mysql是卸载后重 ...

  6. 简单的UIButton按钮动画效果iOS源码

    这个是简单的UIButton按钮动画效果案例,源码,简单的UIButton按钮动画,可以自定义button属性. 效果图: <ignore_js_op> 使用方法: 使用时把ButtonA ...

  7. win2008系统日志不断出现【审核失败】

    win2008系统日志不断出现[审核失败] [现象] 今天查看windows日志,在  -安全-  发现不断有消息刷出,显示  -审核失败-  事件ID为4624 的记录  每分钟大概刷新8条消息(如 ...

  8. Deutsch lernen (05)

    1. die Wahrheit, -en 真理:  - 真言,实情 Wir sollen die Wahrheit festhalten. 坚持:紧握 Im Wein liegt Wahrheit. ...

  9. 团体程序设计天梯赛-练习集-L1-042. 日期格式化

    L1-042. 日期格式化 世界上不同国家有不同的写日期的习惯.比如美国人习惯写成“月-日-年”,而中国人习惯写成“年-月-日”.下面请你写个程序,自动把读入的美国格式的日期改写成中国习惯的日期. 输 ...

  10. 团体程序设计天梯赛-练习集-L1-036. A乘以B

    L1-036. A乘以B 看我没骗你吧 —— 这是一道你可以在10秒内完成的题:给定两个绝对值不超过100的整数A和B,输出A乘以B的值. 输入格式: 输入在第一行给出两个整数A和B(-100 < ...