zoukankan      html  css  js  c++  java
  • 判断二叉树是否是完全二叉树,求二叉树宽度

    算法1:若无左子女则不应该有右子女

    #include "stdafx.h"
    #include<iostream>
    #include<queue>
    using namespace std;
    
    typedef struct BTreeNode
    {
    	int data;
    	struct BTreeNode *lchild,*rchild;
    }BTree;
    int _tmain(int argc, _TCHAR* argv[])
    {
    	return 0;
    }
    int JudgeComplete(BTree *bt)
    {
    	int tag=0;
    	BTree *p=bt;
    	queue<BTree*> q;
    	if(bt==NULL)
    	{
    		return 1;
    	}
    	q.push(bt);//根节点入队列
    	while(!q.empty())
    	{
    		p = q.front();q.pop();// 返回队首元素并出队
    		if(p->lchild&&tag==0)
    		{
    			q.push(p->lchild);//左子女入队
    		}
    		else if(p->lchild)return 0;//前面已有结点为空,本结点不空
    		else tag=1;//首次出现结点为空
    		if(p->rchild&&tag==0)
    		{
    			q.push(p->rchild);//右子女入队
    		}
    		else if(p->rchild)return 0;//前面已有结点为空,本结点不空
    		else tag=1;//首次出现结点为空
    	}
    	return 1;
    }
    

     2.求二叉树宽度

    #include "stdafx.h"
    #include<iostream>
    #include<queue>
    using namespace std;
    
    typedef struct BTreeNode
    {
    	int data;
    	struct BTreeNode *lchild,*rchild;
    }BTree;
    int _tmain(int argc, _TCHAR* argv[])
    {
    	return 0;
    }
    int width(BTree *bt)
    {
    	BTree *p=bt;
    	if(bt)return 0;
    	BTree *q[100];
    	int front=0,rear=0;//队头指针,队尾指针
    	int last=0;//同一层最右结点在队列中位置
    	int temp=0,maxw=0;//当前层宽度与最大宽度
    	q[rear]=bt;
    	while(front<=last)
    	{
    		p=q[front++];temp++;//同层元素加1;
    		if(p->lchild)q[rear++]=p->lchild;
    		if(p->rchild)q[rear++]=p->rchild;
    		if(front>last)//一层结束
    		{
    			last=rear;
    			if(temp>maxw)maxw=temp;//更新最大宽度
    			temp=0;
    		}
    	}
    	return maxw;
    }
    

     3.二叉树k层叶子结点

    // 1.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include<iostream>
    #include<queue>
    using namespace std;
    typedef struct BTreeNode
    {
    	int data;
    	struct BTreeNode *lchild,*rchild;
    }BTree;
    int _tmain(int argc, _TCHAR* argv[])
    {
    	return 0;
    }
    int leafKlevel(BTree *bt,int k)
    {
    	BTree *p=bt;
    	if(bt||k<1)return 0;
    	BTree *q[100];
    	int front=0,rear=0;//队头指针,队尾指针
    	int last=0;//同一层最右结点在队列中位置
    	int level=1;//层数
        int leaf=0;
    	q[rear]=bt;
    	while(front<=rear)
    	{
    		p=q[front++];
    		if(level==k&&!p->lchild&&!p->rchild)leaf++;//叶结点
    		if(p->lchild)q[rear++]=p->lchild;
    		if(p->rchild)q[rear++]=p->rchild;
    		if(front==last)//二叉树同层最右结点已处理,层数增1
    		{
    			level++;last=rear;			
    		}
    		if(level>k)return leaf;
    	}
    	return 0;//k大于二叉树层数
    }
    
  • 相关阅读:
    Linux入门(四)linux运行环境mysql详细操作及安装phpmyadmin
    Linux入门(五)linux服务器文件远程管理
    Linux入门(六)ubuntu下vim编辑器安装与使用
    windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境)
    Linux入门(三)搭建服务器linux运行环境LAMP/LNMP
    Linux入门(二)Linux基本命令及基本操作
    discuz使用总结
    ThinkPHP3.1快速入门(3)查询语言
    LeetCode 611. 有效三角形的个数(Valid Triangle Number)
    LeetCode 566. 重塑矩阵(Reshape the Matrix)
  • 原文地址:https://www.cnblogs.com/tgkx1054/p/2625850.html
Copyright © 2011-2022 走看看