针对考研的C语言学习(2014二叉树大题代码实战)

题目描述

解析

1.递归思想遍历节点,若是叶子结点就累加计算的wpl,反之继续遍历

2.代码如下

复制代码
//树
typedef struct trees {
    ElemType data;
    struct trees* lc;
    struct trees* rc;
}treeNode, * Tree;

3.算法设计

复制代码
//deep路径长度也叫做深度,0开始
void getWPL(Tree& t, int deep,int &sum)
{
    //判断左子树
    if (t->lc)
    {
        getWPL(t->lc, deep + 1, sum);
    }
    if (t->rc)
    {
        getWPL(t->rc, deep + 1, sum);
    }
    if (!t->lc && !t->rc)
    {
        //叶子结点
        sum += deep * t->data;
        return;
    }
}

【注】递归一定要有终止条件,否则会死循环。

可运行代码

复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef char ElemType;

//树
typedef struct trees {
    ElemType data;
    struct trees* lc;
    struct trees* rc;
}treeNode, * Tree;

//链表
typedef struct Links {
    Tree tree;
    struct Links* next;
}LNode, * LinkList;

//队列
typedef struct {
    LinkList front;
    LinkList rear;
}LinkQue;

void init_que(LinkQue& q)
{
    q.front = q.rear = (LinkList)calloc(1, sizeof(LNode));
    q.front = q.rear;
}

bool isEmpty(LinkQue& q)
{
    return q.front == q.rear;
}

//入队
void push_que(LinkQue& q, Tree tree)
{
    //新建链表节点
    LinkList newList = (LinkList)calloc(1, sizeof(LNode));
    newList->next = NULL;
    newList->tree = tree;
    q.rear->next = newList;
    q.rear = q.rear->next;
}
bool pop_que(LinkQue& q, Tree& tree)
{
    if (isEmpty(q))
    {
        return false;
    }
    LinkList del = q.front->next;//头结点不存数据,第一个节点才是真的数据起始位置
    q.front->next = del->next;//断链
    tree = del->tree;
    if (q.rear == del)//只剩下尾节点的数据
    {
        q.rear = q.front;//置空
    }
    free(del);
    return true;
}

void build_tree(Tree& tree)
{
    LinkQue q;
    init_que(q);
    LinkList cur = NULL;
    ElemType data;
    while (scanf("%c", &data) && data != '\n')
    {
        Tree newTree = (Tree)calloc(1, sizeof(treeNode));//申请新的树的节点
        newTree->data = data;
        if (tree == NULL)
        {
            tree = newTree;
            push_que(q, tree);//入队
            cur = q.rear;
        }
        else
        {
            if (cur->tree->lc == NULL)
            {
                cur->tree->lc = newTree;
                push_que(q, newTree);
            }
            else
            {
                cur->tree->rc = newTree;
                push_que(q, newTree);
                //改变当前父亲节点
                cur = cur->next;
            }
        }
    }
}


void getWPL(Tree& t, int deep,int &sum)
{
    //判断左子树
    if (t->lc)
    {
        getWPL(t->lc, deep + 1, sum);
    }
    if (t->rc)
    {
        getWPL(t->rc, deep + 1, sum);
    }
    if (!t->lc && !t->rc)
    {
        //叶子结点
        sum += deep * t->data;
        return;
    }
}

int main()
{
    Tree tree = NULL;
    build_tree(tree);
    int sum = 0;
    getWPL(tree, 0, sum);
    printf("%d\n", sum);
    return 0;
}

代码运行结果

相关推荐
晨非辰2 小时前
《剑指Offer:单链表操作入门——从“头删”开始破解面试》
c语言·开发语言·数据结构·c++·笔记·算法·面试
悠哉悠哉愿意5 小时前
【ROS2学习笔记】 TF 坐标系
笔记·学习·ros2
代码萌新知6 小时前
设计模式学习(五)装饰者模式、桥接模式、外观模式
java·学习·设计模式·桥接模式·装饰器模式·外观模式
驱动探索者8 小时前
find 命令使用介绍
java·linux·运维·服务器·前端·学习·microsoft
小雨凉如水9 小时前
k8s学习-pod的生命周期
java·学习·kubernetes
王夏奇9 小时前
C语言中#pragma的用法
c语言·开发语言
charlie1145141919 小时前
理解C++20的革命特性——协程支持2:编写简单的协程调度器
c++·学习·算法·设计模式·c++20·协程·调度器
文火冰糖的硅基工坊9 小时前
[人工智能-综述-21]:学习人工智能的路径
大数据·人工智能·学习·系统架构·制造
JJJJ_iii9 小时前
【深度学习01】快速上手 PyTorch:环境 + IDE+Dataset
pytorch·笔记·python·深度学习·学习·jupyter
好奇龙猫10 小时前
日语学习-日语知识点小记-进阶-JLPT-N1阶段应用练习(5):语法 +考え方18+2022年7月N1
学习