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)三个运算符优先级顺序是:-最高,*其 ... 
随机推荐
- javascript必须知道的知识要点(二)
			该文章不详细叙述各知识要点的具体内容,仅把要点列出来,供大家学习的时候参照,或者检测自己是否熟练掌握了javascript,清楚各个部分的内容. 内建对象可划分为数据封装类对象.工具类对象.错误类对象 ... 
- poj3233Matrix Power Series(矩阵乘法)
			Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23187 Accepted: ... 
- java 中接口的概念
			接口接口在java中是一个抽象的类型,是抽象方法的集合,接口通常使用interface来声明,一个类通过继承接口的方式从而继承接口的抽象方法.接口并不是类,编写接口的方式和类的很相似,但是他们属于不同 ... 
- [Apple开发者帐户帮助]五、管理标识符(4)注册一个应用程序组
			您需要注册一个或多个组才能启用应用组. 所需角色:帐户持有人或管理员. 在“ 证书”,“标识符和配置文件”中,从左侧的弹出菜单中选择操作系统. 在“标识符”下,选择“应用程序组”,然后单击右上角的“添 ... 
- Django 安装步骤
			Django的安装和简单使用 -安装: pip3 install django==1.11.9 pycharm 下安装,选择版本号, -使用: 命令创建项目:django-admin startpro ... 
- getField();在TP5里成什么了?
			拆分为value和column了 $comps=db("company")->where(array("areaid"=>$areaid))-> ... 
- JavaScript学习二
			2019-05-30 15:08:24 加油,这几天在赶高数,都…… <!DOCTYPE html> <html> <head> <script type=& ... 
- prim解决最小生成树问题
			#include <iostream> #include <algorithm> #include <stdio.h> #include <math.h> ... 
- wpf,vb,位图剪裁的方法
			‘ 貌似WPF对GDI+不提供支持,要达到剪裁图像的方法,可以使用image.clip,’不过clip只是对图片的一个遮挡拦截效果,并不改变本身的图片资源.‘下面的代码提供了剪裁图片资源的方法. Di ... 
- [2月1号] 努比亚全机型ROM贴  最全最新NubiaUI5.0  ROOT  极速体验
			前言 感谢在开发过程中mandfx和dgtl198312予以的帮助!本帖将整理所有Nubia手机的最新刷机包,还有少数机型未制作刷机包,需要的机油可以联系我制作recovery以及刷机包加群23722 ... 
