leetcode 513.找树左下角的值

1.题目要求:

代码块:

c 复制代码
给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。

假设二叉树中至少有一个节点。

 

2.此题思路:

1.创建队列,出队函数和入队函数:

c 复制代码
//创建队列
typedef struct queuet{
    struct TreeNode* value;
    struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){
    queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));
    newnode->value = data;
    newnode->next = NULL;
    if(*head == NULL){
        *head = newnode;
        return;
    }
    queue_t* tail = *head;
    while(tail->next != NULL){
        tail = tail->next;
    }
    tail->next = newnode;
}
//出队
struct TreeNode* pop(queue_t** head){
    struct TreeNode* x = (*head)->value;
    (*head) = (*head)->next;
    return x;
}

2.创建两个数组,一个用于层序遍历,一个用于记录树每层的结点个数:

c 复制代码
queue_t* enquence = NULL;
    int size = 0;
    int count = 1;//当前行的结点数
    int nextcount = 0;//记录树下一行的结点数
    int* number = (int*)malloc(sizeof(int) * 10000);//用来层序遍历
    int j1 = 0;
    int* low = (int*)malloc(sizeof(int) * 10000);//用来记录树每层的结点个数
    int j2 = 0;
  1. 层序遍历:
c 复制代码
push(&enquence,root);//入队
    size++;
    //进行层序遍历
    while(size != 0){ 
        for(int i = 0;i <  count;i++){
            struct TreeNode* temp = pop(&enquence);//出队
            size--;
            number[j1] = temp->val;//往数组里存入结点值
            j1++;
            if(temp->left != NULL){
                push(&enquence,temp->left);//入队
                nextcount++;//此代表下一行的节点数
                size++;
            }
            if(temp->right != NULL){
                push(&enquence,temp->right);//入队
                nextcount++;
                size++;
            }
        }
        low[j2] = count;
        j2++;
        count = nextcount;
        nextcount = 0;
    }

4.取记录行结点数的数组的最后一个,最后让另一个数组,从后向前走,直到 等于记录节点数的最后一个:

c 复制代码
count = low[j2 - 1];
    int count1 = 1;
    for(int i = j1 - 1;i >= 0;i--){
        if(count1 == count){
            return number[i]; 
        }
        count1++;
    }
    return 0;

全部代码:

c 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 //创建队列
typedef struct queuet{
    struct TreeNode* value;
    struct queue* next;
}queue_t;
//入队
void push(queue_t** head,struct TreeNode* data){
    queue_t* newnode = (queue_t*)malloc(sizeof(queue_t));
    newnode->value = data;
    newnode->next = NULL;
    if(*head == NULL){
        *head = newnode;
        return;
    }
    queue_t* tail = *head;
    while(tail->next != NULL){
        tail = tail->next;
    }
    tail->next = newnode;
}
//出队
struct TreeNode* pop(queue_t** head){
    struct TreeNode* x = (*head)->value;
    (*head) = (*head)->next;
    return x;
}
int findBottomLeftValue(struct TreeNode* root) {
    queue_t* enquence = NULL;
    int size = 0;
    int count = 1;//当前行的结点数
    int nextcount = 0;//记录树下一行的结点数
    int* number = (int*)malloc(sizeof(int) * 10000);//用来层序遍历
    int j1 = 0;
    int* low = (int*)malloc(sizeof(int) * 10000);//用来记录树每层的结点个数
    int j2 = 0;
    push(&enquence,root);
    size++;
    //进行层序遍历
    while(size != 0){ 
        for(int i = 0;i <  count;i++){
            struct TreeNode* temp = pop(&enquence);
            size--;
            number[j1] = temp->val;
            j1++;
            if(temp->left != NULL){
                push(&enquence,temp->left);
                nextcount++;
                size++;
            }
            if(temp->right != NULL){
                push(&enquence,temp->right);
                nextcount++;
                size++;
            }
        }
        low[j2] = count;
        j2++;
        count = nextcount;
        nextcount = 0;
    }
    count = low[j2 - 1];
    int count1 = 1;
    for(int i = j1 - 1;i >= 0;i--){
        if(count1 == count){
            return number[i]; 
        }
        count1++;
    }
    return 0;
}

我这个时间复杂度较高,但大家如果觉得好的话,就请给个免费的赞吧,谢谢了

^ _ ^

相关推荐
BB_CC_DD19 分钟前
四. 以Annoy算法建树的方式聚类清洗图像数据集,一次建树,无限次聚类搜索,提升聚类搜索效率。(附完整代码)
深度学习·算法·聚类
梁下轻语的秋缘2 小时前
每日c/c++题 备战蓝桥杯 ([洛谷 P1226] 快速幂求模题解)
c++·算法·蓝桥杯
CODE_RabbitV2 小时前
【深度强化学习 DRL 快速实践】逆向强化学习算法 (IRL)
算法
mit6.8242 小时前
[贪心_7] 最优除法 | 跳跃游戏 II | 加油站
数据结构·算法·leetcode
keep intensify2 小时前
通讯录完善版本(详细讲解+源码)
c语言·开发语言·数据结构·算法
shix .2 小时前
2025年PTA天梯赛正式赛 | 算法竞赛,题目详解
数据结构·算法
风铃儿~3 小时前
Java面试高频问题(26-28)
java·算法·面试
wuqingshun3141593 小时前
蓝桥杯 4. 卡片换位
算法·职场和发展·蓝桥杯
江沉晚呤时3 小时前
深入了解C# List集合及两种常见排序算法:插入排序与堆排序
windows·sql·算法·oracle·c#·排序算法·mybatis
Eric.Lee20213 小时前
数据集-目标检测系列- F35 战斗机 检测数据集 F35 plane >> DataBall
人工智能·算法·yolo·目标检测·计算机视觉