代码随想录算法训练营第21天 | 513. 找树左下角的值 |112. 路径总和 | 106. 从中序与后序遍历序列构造二叉树

513. 找树左下角的值

题目链接

解:层序遍历

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

struct queue {
    int front, back;
    struct TreeNode *array[10000];
};

int findBottomLeftValue(struct TreeNode* root) {
    struct queue *que = (struct queue *)malloc(sizeof(*que));
    struct TreeNode *node = root;
    int ret = 0;

    memset(que, 0, sizeof(*que));
    que->front = que->back = 0;

    if (root == NULL) {
        return ret;
    }

    que->array[que->back++] = node;

    while (que->front != que->back) {
        int tmp = que->back;
        ret = que->array[que->front]->val;
        while (que->front != tmp) {
            node = que->array[que->front];
            if (node->left != NULL) que->array[que->back++] = node->left;
            if (node->right != NULL) que->array[que->back++] = node->right;
            que->front++;
        }
    }

    return ret;
}

112. 路径总和

题目链接

解: 递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

bool pathsum(struct TreeNode *node, int target) {
    if (node == NULL) return false;
    if (node->left == NULL && node->right == NULL && node->val == target) {
        return true;
    }

    bool left = pathsum(node->left, target - node->val);
    bool right = pathsum(node->right, target - node->val);

    return left | right;
}

bool hasPathSum(struct TreeNode* root, int targetSum) {
    return pathsum(root, targetSum);
}

解:迭代

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

struct stacknode {
    struct TreeNode *node;
    int sum;
};

struct stack {
    int top;
    struct stacknode stacknode[1000];
};

void push(struct stack *st, struct TreeNode *node, int sum) {
    if (st->top == 99) {
        printf("stack overflow\n");
        exit(1);
    }

    st->stacknode[++st->top].node = node;
    st->stacknode[st->top].sum = sum;
}

struct stacknode pop(struct stack *st) {
    return st->stacknode[st->top--];
}

bool hasPathSum(struct TreeNode* root, int targetSum) {
    struct stack *st = (struct stack *)malloc(sizeof(*st));
    struct TreeNode *node = root;

    memset(st, 0, sizeof(*st));
    st->top = -1;

    if (root == NULL) return false;

    push(st, node, node->val);

    while (st->top != -1) {
        struct stacknode cur = pop(st);
        node = cur.node;

        if (node->right != NULL) {
            push(st, node->right, cur.sum + node->right->val);
        }

        if (node->left != NULL) {
            push(st, node->left, cur.sum + node->left->val);
        }

        if (node->left == NULL && node->right == NULL) {
            if (cur.sum == targetSum) return true;
        }
        
    }

    return false;
}

106. 从中序与后序遍历序列构造二叉树

题目链接

解: 递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

struct TreeNode *build_tree(int *inorder, int instart, int inend, int *postorder, int poststart, int postend) {
    if (instart > inend || poststart > postend) {
        return NULL;
    }

    int rootvalue = postorder[postend];
    struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    root->val = rootvalue;
    root->left = NULL;
    root->right = NULL;

    int rootidx;
    for (rootidx = instart; rootidx <= inend; rootidx++) {
        if (inorder[rootidx] == rootvalue) {
            break;
        }
    }

	// rootidx-1-instart 表示前序有多少个元素, 后序就有多少个元素
    root->left = build_tree(inorder, instart, rootidx-1, postorder, poststart, poststart+rootidx-1-instart);
    root->right = build_tree(inorder, rootidx+1, inend, postorder, poststart+rootidx-instart, postend-1);

    return root;
}

struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize) {
    if (inorderSize <= 0 || postorderSize <= 0) {
        return NULL;
    }

    return build_tree(inorder, 0, inorderSize-1, postorder, 0, postorderSize-1);
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/606294.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

SpringBoot启动流程源码解析

目录 一、SpringApplication构造方法解析 1. web应用类型 2. BootstrapRegistryInitializer 3. ApplicationContextInitializer 4. ApplicationListener 5. 推断Main方法所在类 二、SpringApplication.run(String... args)方法解析 1.创建DefaultBootstrapContext 2.获…

回顾5款我非常喜欢的软件,希望大家也能喜欢

​ 我喜欢分享好软件,这就像与老友聊天一样让我感到快乐。在这个过程中,我可以回顾这些实用的小工具,也希望它们可以帮助到更多人。 1.备份工具——Cobian Backup ​ Cobian Backup是一款功能强大的备份软件&#xff0c;支持自动定时备份、增量备份、差异备份等多种备份方式。…

Java八股文系列之四(JVM)

什么是Java虚拟机&#xff1f;为什么Java被称作是“平台无关的编程语言”&#xff1f; Java虚拟机是一个可以执行Java字节码的虚拟机进程。 Java 实现跨平台的主要原因在于它的编译和执行方式。Java 程序首先被编译成中间代码&#xff08;bytecode&#xff09;&#xff0c;然…

九泰智库 | 医械周刊- Vol.26

⚖️ 法规动态 国家卫健委等八部门印发《关于加强重症医学医疗服务能力建设的意见》 5月6日&#xff0c;国家卫健委发布《关于加强重症医学医疗服务能力建设的意见》&#xff0c;明确我国重症医学领域的重要部署。数据显示&#xff0c;我国在重症救治设备方面依然存在巨大缺口…

vue中使用element的i18n语言转换(保姆式教程-保证能用)

1、项目中需要使用的插件&#xff0c;vue2或vue3、element、vue-i18n、js-cookie、vuex我是在vue2中使用 npm i element-ui -S npm i js-cookie -S npm i vue-i18n8.28.2 //因为我项目使用的vue2&#xff0c;直接安装报错了,就下载了固定的版本2、在main.js中引入i18n impor…

芸众商城电商专业版400+插件源码+搭建教程

介绍&#xff1a; 芸众商城社交电商系统SAAS平台前端基于vue开发&#xff0c;后端基于研发积分商城系统源码 php&#xff0c;本文安装芸众商城全插件&#xff08;400多个&#xff09;商业版平台源码&#xff0c;可同时支持多端口部署运行&#xff1b;使用宝塔面板一键部署的形…

出货300万片后,智舱界「小高通」浮出水面

‍作者 |张祥威 编辑 |德新 2024年北京车展&#xff0c;本土芯片公司开始截击外企供应商。 很长一段时间内&#xff0c;汽车行业智驾芯片看英伟达&#xff0c;座舱芯片看高通。英伟达Orin系列广受欢迎&#xff0c;高通8155席卷主流智能汽车&#xff0c;8295更是被视为最强配置…

倍思|西圣开放式耳机哪个好用?热门机型深度测评!

在数字化生活的浪潮中&#xff0c;耳机已成为我们不可或缺的伴侣。然而&#xff0c;长时间佩戴传统的耳机容易导致的耳道疼痛等问题&#xff0c;严重的话将影响听力。许多人开始寻找更为舒适的佩戴体验。开放式耳机因为不需要需直接插入耳道的设计&#xff0c;逐渐受到大众的青…

不会pdf修改编辑文字怎么办?看完秒懂

不会pdf修改编辑文字怎么办&#xff1f;在日常生活中&#xff0c;PDF文件已成为我们工作、学习不可或缺的一部分。然而&#xff0c;很多人对PDF文件的编辑操作感到困惑&#xff0c;尤其是修改其中的文字。今天&#xff0c;我们就来详细解析一下&#xff0c;不会PDF修改编辑文字…

全局变量在 Python 中的应用场景

在Python中&#xff0c;全局变量是在程序的全局范围内定义的变量&#xff0c;可以在整个程序中访问。虽然在Python中使用全局变量并不像在其他编程语言中那样被推荐&#xff0c;因为它可能导致代码不易理解和维护&#xff0c;但在一些特定的情况下&#xff0c;全局变量仍然是有…

企业微信hook接口协议,ipad协议http,设置是否自动同意

设置是否自动同意 参数名必选类型说明uuid是String每个实例的唯一标识&#xff0c;根据uuid操作具体企业微信 请求示例 {"uuid":"bc4800492083fdec4c1a7e5c94","state":1 //1 是需要验证同意&#xff08;需要手动点击同意&#xff09; 0关闭验证…

gtest的编译与使用

文章目录 gtest的编译与使用概述笔记CMake参数官方文档测试程序测试效果END gtest的编译与使用 概述 gTest是 googletest的缩写&#xff0c;如果直接找gTest项目&#xff0c;是找不到的。 库地址 https://github.com/google/googletest.git 迁出到本地后&#xff0c;切到最新…

中电金信:看 “咨询+技术”如何引领数字化变革新风向

当前&#xff0c;新一轮创新技术和产业变革正在重塑全球的经济格局。日本政府及社会各界也从各个领域着手推进数字化。2021年&#xff0c;日本政府成立了“数字厅”&#xff0c;通过一系列举措推动数字化升级&#xff0c;希望将日本加速转型为数字经济的区域领导者&#xff0c;…

继续SQL

主知识点六&#xff1a;having 聚合前的筛选用where&#xff0c;聚合后的筛选用having Having和where的区别是&#xff1a;运行顺序和对象不用 Having是在group by聚合后的基础上进行筛选。 ● 【例题27*】&#xff08;运行原理&#xff09;查询总人口数至少为3亿的大洲和…

git的标签管理

理解标签 在Git中,标签tag用于标记特定的一个重要点&#xff0c;比如版本发布。标签允许捕捉某一次提交的状态&#xff0c;当我们需要退回到某次提叫的版本时&#xff0c;通过标签我们快速定位到。标签具有两种类型&#xff1a; 轻量标签&#xff1a;最简单的标签形式&#x…

QGIS编译

一&#xff0c;安装&#xff1a;OSGeo4W 二&#xff0c;安装&#xff1a;Cygwin64 https://www.cygwin.com/setup-x86_64.exe 三&#xff0c;安装&#xff1a; 安装bison和flex 四&#xff09;QGIS_3.28 下载QGIS_3.28的源码包 五 环境变量设置&#xff1a; echo off set VS19…

那些可免费使用的在线大语言模型服务

2022年底以ChatGPT[1]为代表的大语言模型的出现掀起了人工智能应用的新浪潮。这些庞大的语言模型经过对海量文本数据的训练&#xff0c;能够理解和生成逼近人类水平的自然语言&#xff0c;在对话、问答、文本生成、代码编写等领域展现出了惊人的能力。 最初这种能力“垄断”在O…

用手势掌控PPT,玩转演示新姿势

推荐运行环境 使用anaconda创建环境&#xff0c;以免污染原来的python开发环境conda install python3.9pip install -q mediapipe0.10.0pip install pyautoguiPython: version 3.8 - 3.11PIP: version 20.3 请注意以下的坑 以下为我测试过程中的大坑&#xff0c;请及时避开&am…

【2024高校网络安全管理运维赛】巨细记录!

2024高校网络安全管理运维赛 文章目录 2024高校网络安全管理运维赛MISC签到考点&#xff1a;动态图片分帧提取 easyshell考点&#xff1a;流量分析 冰蝎3.0 Webphpsql考点&#xff1a;sql万能钥匙 fileit考点&#xff1a;xml注入 外带 Cryptosecretbit考点&#xff1a;代码阅读…

Driftingblues靶机系列Driftingblues5

获取靶机的ip&#xff1a;192.168.108.37 扫描靶机的端口服务: 看到web服务和ssh服务&#xff1a; 先查看一下web服务&#xff1a; 扫描到web服务的信息&#xff1a; 访问web服务&#xff1a; 在源代码中并没有看到有什么新的信息&#xff0c;扫描一下靶机目录&#xff1a;…
最新文章