判断二叉树是否对称、道路规划、求完全二叉树度为1和0结点的个数--c++【做题记录】

判断二叉树是否对称

【问题描述】用前序字符串建立一个字符型二叉树,判断这个二叉树是否对称。

   1 

  / \ 

2    2 

/ \  / \ 

3 4 4 3 

如上图,二叉树中左右子树对称,则二叉树对称。

【输入形式】扩展的前序字符串
【输出形式】对称输出true,不对称输出false
【样例输入】123##4##24##3##

【样例输出】 true 

【样例输入】123##4##23##4##

【样例输出】false 

【提示】

判断一个树是否对称等价于判断左右子树是否对称。

(1)如果两个子树都为空指针,则它们相等或对称

(2) 如果两个子树只有一个为空指针,则它们不相等或不对称

(3)如果两个子树根节点的值不相等, 则它们不相等或不对称

(4)根据相等或对称要求,进行递归处理。

【代码】

#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;
const int MAX=20;

struct BiNode
{
	char data;
	BiNode *lchild,*rchild;
};
BiNode *root=NULL;

BiNode* creat(BiNode* bt)
{
	char ch;
	cin>>ch;
	if(ch=='#')
	{
		bt=NULL;
	}
	else
	{
		bt=new BiNode;
		bt->data=ch;
		bt->lchild=creat(bt->lchild);
		bt->rchild=creat(bt->rchild);
	}
	return bt;
}

bool isSame(BiNode *bt1, BiNode *bt2)
{
	if (!bt1 && !bt2) {  //两个结点都为空,则对称
        return true;
    }
    if (!bt1 || !bt2) {  //只有一个结点为空,不对称
        return false;
    }
    if (bt1->data != bt2->data) {  //两个结点的数据不相等,则不对称
        return false;
    }
	//继续比较这两个结点的左右子树是否相等
    return isSame(bt1->lchild, bt2->rchild) && isSame(bt1->rchild, bt2->lchild);
}

int main()
{
	root=creat(root);
	if(isSame(root->lchild,root->rchild))
		cout<<"true";
	else
		cout<<"false";
	return 0;
}

道路规划

【问题描述】

n个村庄之间有m条有权无向道路,使得每个村庄都能到达任意村庄,现让你重新进行道路规划,在保证每个村庄都能到达任意村庄的情况下,删除一定数量的道路,使得删除的道路权值和最大。

【输入形式】

第1行两个数n, m,分别表示村庄数和道路数。(n<= 1000, m <= min(n * n, 50000) )

第2至m+1行,每行三个数u, v, w,表示村庄u和村庄v之间有一条权值为w的无向道路。

(1 <= u, v <= n, u != v, 0 <= w <= 1000且保证不存在重边)

【输出形式】

一行,一个数字,表示最大删除权值和。

【样例输入】

4 5

1 2 2

1 3 2

1 4 3

2 3 4

3 4 3

【样例输出】

7

【代码】

#include <iostream>
#include <limits.h>
#include <string> // 包含string以便处理顶点的字符表示
#define MAX_V_NUM 20

using namespace std;

struct closedge
{
    char adjvex; // 邻接点的字符表示
    int lowcost; // 到邻接点的最小权值
};

closedge shortEdge[MAX_V_NUM]; // 存储候选最短边集

class MGraph
{
private:
    char vertex[MAX_V_NUM]; // 图的顶点数组
    int arc[MAX_V_NUM][MAX_V_NUM]; // 图的邻接矩阵
    int vexNum, arcNum; // 顶点数和边数
    int weight[MAX_V_NUM] = { 0 };
    int result = 0; //权值之和
public:
    MGraph(); // 构造函数
    void Prim(int start); // 实现Prim算法
};

MGraph::MGraph()
{
    cout << "请输入顶点的个数:";
    cin >> vexNum;
    //cout << "请依次输入" << vexNum << "个顶点的值:" << endl;
    /*for (int i = 0; i < vexNum; i++)
    {
        vertex[i]=('1'+i);
    }*/
    cout << "请输入边的个数:";
    cin >> arcNum;
    char vi, vj;
    int w;
    // 初始化邻接矩阵
    for (int i = 0; i < vexNum; i++)
    {
        for (int j = 0; j < vexNum; j++)
        {
            if (i == j)
            {
                arc[i][j] = 0;
            }
            else
            {
                arc[i][j] = INT_MAX;
            }
        }
    }
    cout << "请依次输入" << arcNum << "个边的起点、终点和权值:" << endl;
    for (int i = 0; i < arcNum; i++)
    {
        cin >> vi >> vj >> w;
        arc[vi - '1'][vj - '1'] = w;
        arc[vj - '1'][vi - '1'] = w; // 因为是无向图
        weight[i] = w;
        result += w;
    }
    // 假设从顶点0开始Prim算法
    Prim(0);
}

void MGraph::Prim(int start)
{
    // 初始化shortEdge数组
    for (int i = 0; i < vexNum; i++)
    {
        shortEdge[i].lowcost = arc[start][i];
        shortEdge[i].adjvex = start;
    }
    shortEdge[start].lowcost = 0;
    //cout << "最小生成树是:" << endl;
    for (int i = 0; i < vexNum - 1; i++)
    {
        int k = 0;
        for (int j = 0; j < vexNum; j++)
        {
            if (shortEdge[j].lowcost != 0 && shortEdge[j].lowcost != INT_MAX) {
                k = j;
                break;
            }
        }
        for (int j = 0; j < vexNum; j++) {
            if (shortEdge[j].lowcost != 0 && shortEdge[k].lowcost > shortEdge[j].lowcost)
            {
                k = j;
                break;
            }
        }
        // 输出最小边
        //cout << "(" << vertex[shortEdge[k].adjvex] << vertex[k] << ")" << shortEdge[k].lowcost << endl;
        result -= shortEdge[k].lowcost;  //用权值总和减去用于生成树的边的权值,剩下的就是要删除的权值
        // 将找到的最小边标记为0,表示已访问
        shortEdge[k].lowcost = 0;
        weight[k] = 0;
        for (int j = 0; j < vexNum; j++)
        {
            if (arc[k][j] != 0 && arc[k][j] < shortEdge[j].lowcost)
            {
                shortEdge[j].lowcost = arc[k][j];
                shortEdge[j].adjvex = k;
            }
        }
    }
    
    cout << "删除权值为" << result << endl;
}

int main()
{
    MGraph graph;
    return 0;
}

求完全二叉树度为0和1的结点个数

#include <iostream>
using namespace std;

int count_nodes(int n) {
    // 计算叶子节点数量
    int leaf_count =(n+1)/ 2;
    // 计算内部节点数量
    int internal_count ;
    if(n%2==0)
        internal_count=1;
    else
        internal_count=0;
    return leaf_count + internal_count;
}

int main() {
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        int m;
        cin >> m;
        cout << count_nodes(m) << endl;
    }
    return 0;
}


 

 

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值