#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. 0604-面向对象、类与对象、类、static、构造方法/析构方法

    一.面向对象 1.面向过程:一个人分步骤完成某个事情 2.面向对象:某件事情拆分为多个任务,由每个对象独立完成,最后调用整合为一个完整的项目 3.三要素:继承.封装.多态. 封装:私有化属性 提供公共 ...

  2. 0420-mysql命令(数据库操作层级,建表,对表的操作)

    注意事项: 符号必须为英文. 数据库操作层级: 建表大全: #新建表zuoye1:drop table if exists zuoye1;create table zuoye1(    id int ...

  3. go之map

    一.概念 简述 1.map 是一种无序的键值对的集合.(类似于python的字典dict) 2.map 的key 与 value 都是有类型的,且定义阶段时就已经统一 定义方式 # 方式一 var m ...

  4. WebService常用技术及术语

    一.Web Service是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨平台的规范(抽象) 3. 多个跨平台.跨语言的应用间通信整合的方案(实 ...

  5. python修改植物僵尸

    import win32process#进程模块 import win32con#系统定义 import win32api#调用系统模块 import ctypes#C语言类型 import win3 ...

  6. Cookie、Token与Session介绍(非原创)

    文章大纲 一.Cookie介绍二.Token介绍三.Session介绍四.Token.Cookie与Session比较五.参考文章   一.Cookie介绍 1. Cookie是什么 cookie机制 ...

  7. centos7安装python3.7和ipython

    一.centos7为刚安装的 1)配置yum源和epel源 采用国内源 查看yum的配置文件 (里面的镜像网址)是否ping的通 全部更改成 国内的 yum .epel源 在图中位置 下载相应的 re ...

  8. 命令行启动ubuntu

    图形模式下,首先进入终端: 1. 运行 sudo vi/etc/default/grub 2. 找到 GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash” 3.改为 GR ...

  9. Halcon学习笔记之支持向量机(一)

    例程:class_overlap_svm.hdev 说明:这个例程展示了如何用一个支持向量机来给一幅二维的图像进行分类.使用二维数据的原因是因为它可以很容易地联想成为区域和图像.本例程中使用了三个互相 ...

  10. Redis学习笔记(三)-数据类型之string类型

    string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象.从内部实现来看其实string可以看作byte ...