C++二叉树笔试题
#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++二叉树笔试题的更多相关文章
- C/C++ 笔试题
/////转自http://blog.csdn.net/suxinpingtao51/article/details/8015147#userconsent# 微软亚洲技术中心的面试题!!! 1.进程 ...
- 嵌入式Linux C笔试题积累(转)
http://blog.csdn.net/h_armony/article/details/6764811 1. 嵌入式系统中断服务子程序(ISR) 中断是嵌入式系统中重要的组成部分,这导致了很 ...
- C/C++笔试题(很多)
微软亚洲技术中心的面试题!!! .进程和线程的差别. 线程是指进程内的一个执行单元,也是进程内的可调度实体. 与进程的区别: (1)调度:线程作为调度和分配的基本单位,进程作为拥有资源的基本单位 (2 ...
- 阿里巴巴2013年实习生笔试题B
阿里巴巴集团2013实习生招聘技术类笔试题(B) 一.单向选择题 1.在常用的网络协议中,___B__是面向连接的.有重传功能的协议. A. IP B. TCP C. UDP D. DXP 2.500 ...
- 华为C语言笔试题集合
①华为笔试题搜集 1.static有什么用途?(请至少说明两种) 1)在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变. 2) 在模块内(但在函数体外),一个被声明为 ...
- java笔试题13-11-21
中xxx科技公司java笔试题 今天去参加一个公司的面试,去先做了一份笔试题,妈的,太他妈难了(对于我来说,最后做完一个员工说你是不是投错简历了,都是空白,我说我做的大部分都对了..最后面试都没有,就 ...
- java各公司笔试题集1
IBM笔试题 注:IBM笔试题一小时之内完成,题目全部用英文描述,这里用中文表述 一.名词解释 1.Eclipse 2.J2EE 3.EJB 4.Ajax 5.Web service 二.找出以下代码 ...
- google2013校园招聘笔试题(全国)
google2013校园招聘笔试题 1. 单项选择题1.1如果把传输速率定义为单位时间内传送的信息量(以字节计算)多少.关于一下几种典型的数据传输速率:1.使用USB2.0闪存盘,往USB闪存盘上拷贝 ...
- google浙大招聘笔试题 师兄只能帮你到这儿了
google浙大招聘笔试题 一.单选1.80x86中,十进制数-3用16位二进制数表示为?00100002.假定符号-.*.$分别代表减法.乘法和指数运算,且 1)三个运算符优先级顺序是:-最高,*其 ...
随机推荐
- Coursera Algorithms week1 查并集 练习测验:3 Successor with delete
题目原文: Given a set of n integers S = {0,1,…,N-1}and a sequence of requests of the following form: Rem ...
- php的self this parent的区别
{一}PHP中this,self,parent的区别之一this篇 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行P ...
- 【DP】编辑距离
日常吐槽:关于DP,有一种莫名的恐惧...maybe源于与mtw大佬与quantum11大佬,初中时抬老师爬楼梯的经历... 言归正传: 编辑距离 [题目描述] 设A和B是两个字符串.我们要用最少的字 ...
- HTML 5概述
HTML语言是一种简易的文件交换标准,用于物理的文件结构,它旨在定义文件内的对象和描述文件的逻辑结构,而并不定义文件的显示.由于HTML所描述的文件具有极高的适应性,所以特别适合于WWW的出版环境. ...
- Java数据的基本类型
整数类型: byte:8位(1个字节) eg:byte x=2,y: 错误实例:byte b:b=b+3: 其中b=b+3是错误的,应该是b=(byte)(b+3)强制转换: short:16位( ...
- SAP computer之architecture
Simple-As-Possible computer introduces all the cruicial ideas behind computer operation without bury ...
- 【sqli-labs】 less24 POST- Second Order Injections *Real treat* -Stored Injections (POST型二阶注入 *真的好玩?* 存储注入)
简单登陆浏览一遍后,发现是一个登陆注册修改密码的应用 审查一下代码 登陆页面的username,password使用了转义 注册页面的参数也进行了转义处理 但是在修改password的页面,直接从se ...
- JSP_内置对象_session
session表示客户端与服务器的一次会话. Web中的session指的是用户在浏览某个网站时,从进入网站到浏览器关闭所经过的这段时间,也就是用户浏览这个网站所花费的时间. 从上述定义中可以看到,s ...
- Nuxt报错
新增了个文件store.js: //本地存储的工具库 if(!window.localStorage){ alert('你的浏览器不支持localStorage'); } var store ={ s ...
- Asp.Net Core 自动适应Windows服务、Linux服务、手动启动时的内容路径的扩展方法
public static IWebHostBuilder UseContentRootAsEnv(this IWebHostBuilder hostBuilder) { bool IsWindows ...