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)三个运算符优先级顺序是:-最高,*其 ...
随机推荐
- 5-8 第五天 微信 JS-SDK
SDK的难点不多,但是容易出错的地方特别多.几乎任何一个环节都会犯错误. 难道没有域名就不让你测了吗?别担心,有公众测试号, 域名这个代理呢是通过QQ浏览器的服务器调试,来绑定这个端口的,把这个服务开 ...
- git拉取远端改变,但是不覆盖本地的修改
1.git stash 2.git fetch weixin-old-remote 3.git rebase weixin-old-remote/main151028_wxpay_main15100 ...
- js form settimeout
<html><head><meta charset="utf8"><script type="text/javascript&q ...
- bzoj1231[Usaco2008 Nov]mixup2 混乱的奶牛(状压dp)
1231: [Usaco2008 Nov]mixup2 混乱的奶牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1032 Solved: 588[ ...
- 在linux服务器centos上使用svn同步代码到项目中
一.需求 1.在多人开发过程中代码的管理以及版本的控制是一个很重要的问题,因为在开发过程中我们可能会同时更改过某个文件或者更改过多个文件, 这会导致我们很容易发生错误.所以我们需要一个方式去管理我们的 ...
- 5.27 indeed 第三次网测
1. 第一题, 没有看 2. 暴力枚举.每一个部分全排列, 然后求出最大的请求数. #include<bits/stdc++.h> #define pb push_back typedef ...
- # secure_link_module模块
作用 制定并允许检查请求的链接的真实性以及保护资源免遭未经授权的访问 限制链接生效周期 首先检查nginx是否已安装模块 #nginx -V 输出nginx所有已安装模块,检查是否有ngx_http_ ...
- spring事务回滚问题
刚刚接到一个上家公司同事的一个电话,问我为什么service方法事务不会滚了,日志打印了,调用webservice报错. 我让他把这个调用执行webservice的方法截图发给我,如下: publ ...
- TCP/IP,必知必会的
文章目录 前言 TCP/IP模型 数据链路层 网络层 ping Traceroute TCP/UDP DNS TCP连接的建立与终止 TCP流量控制 TCP拥塞控制 0 前言 本文整理了一些TCP/I ...
- 轻量数据库SQLiteDataBase的相关操作方法
一.查询操作: 查询操作比较复杂,主要有如下操作: db.rawQuery(String sql, String[] selectionArgs); db.query(String table, St ...