代码随想录算法训练营第14天 | 144. 二叉树的前序遍历 | 145. 二叉树的后序遍历 | 94. 二叉树的中序遍历

144. 二叉树的前序遍历

解1: 递归

void preorder(struct TreeNode* root, int *array, int *idx) {if (root != NULL) {array[(*idx)++] = root->val;preorder(root->left, array, idx);preorder(root->right, array, idx);}
}int* preorderTraversal(struct TreeNode* root, int* returnSize) {int *array = (int *)malloc(sizeof(int) * 100);memset(array, 0, sizeof(int) * 100);*returnSize = 0;preorder(root, array, returnSize);return array;
}

解2: 迭代

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     struct TreeNode *left;*     struct TreeNode *right;* };*/
/*** Note: The returned array must be malloced, assume caller calls free().*/struct stack{int top;struct TreeNode *stk[100];
};int* preorderTraversal(struct TreeNode* root, int* returnSize) {int *array = (int *)malloc(sizeof(int) * 100);struct stack *st = (struct stack *)malloc(sizeof(*st));struct TreeNode *node = root;memset(st, 0, sizeof(*st));st->top = -1;*returnSize = 0;if (root == NULL) {return array;}while (st->top > -1 || node != NULL) {while (node != NULL) {array[(*returnSize)++] = node->val;st->stk[++st->top] = node;node = node->left;}node = st->stk[st->top--];node = node->right;}printf("%d\n", *returnSize);return array;
}

145. 二叉树的后序遍历

解1: 递归

void postorder(struct TreeNode *root, int *ans, int *returnSize) {if (root != NULL) {postorder(root->left, ans, returnSize);postorder(root->right, ans, returnSize);ans[(*returnSize)++] = root->val;}
}int* postorderTraversal(struct TreeNode* root, int* returnSize) {int *ans = (int *)malloc(sizeof(int) * 100);*returnSize = 0;postorder(root, ans, returnSize);return ans;
}

解2: 迭代

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     struct TreeNode *left;*     struct TreeNode *right;* };*/
/*** Note: The returned array must be malloced, assume caller calls free().*/struct stack{int top;struct TreeNode *stk[100];
};void reverse(int *array, int n) {int left = 0, right = n-1;int tmp;while (left < right) {tmp = array[left];array[left] = array[right];array[right] = tmp;left++, right--;}
}int* postorderTraversal(struct TreeNode* root, int* returnSize) {int *array = (int *)malloc(sizeof(int) * 100);struct stack *st = (struct stack *)malloc(sizeof(*st));struct TreeNode *node = NULL;memset(st, 0, sizeof(*st));st->top = -1;*returnSize = 0;if (root == NULL) {return array;}st->stk[++st->top] = root;while (st->top > -1) {node = st->stk[st->top--];array[(*returnSize)++] = node->val;if (node->left) st->stk[++st->top] = node->left;if (node->right) st->stk[++st->top] = node->right;}reverse(array, *returnSize);return array;
}

94. 二叉树的中序遍历

解1: 递归

void inorder(struct TreeNode* root, int *array, int *idx) {if (root != NULL) {inorder(root->left, array, idx);array[(*idx)++] = root->val;inorder(root->right, array, idx);}
}int* inorderTraversal(struct TreeNode* root, int* returnSize) {int *array = (int *)malloc(sizeof(int) * 100);memset(array, 0, sizeof(int) * 100);*returnSize = 0;inorder(root, array, returnSize);return array;
}

解2: 迭代

/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     struct TreeNode *left;*     struct TreeNode *right;* };*/
/*** Note: The returned array must be malloced, assume caller calls free().*/struct stack{int top;struct TreeNode *stk[100];
};int* inorderTraversal(struct TreeNode* root, int* returnSize) {int *array = (int *)malloc(sizeof(int) * 100);struct stack *st = (struct stack *)malloc(sizeof(*st));struct TreeNode *node = NULL;memset(st, 0, sizeof(*st));st->top = -1;*returnSize = 0;if (root == NULL) {return array;}node = root;while (st->top > -1 || node != NULL) {while (node != NULL) {st->stk[++st->top] = node;node = node->left;}node = st->stk[st->top--];array[(*returnSize)++] = node->val;node = node->right;}return array;
}

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【代码随想录】day49

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 一、121. 买卖股票的最佳时机二、122.买卖股票的最佳时机II 一、121. 买卖股票的最佳时机 class Solution { public:int maxProfit(vector<int>& prices…

Qt QImageWriter类介绍

1.简介 QImageWriter 用于写入图像文件的类。它提供了将 QImage 对象保存到不同图像格式文件的功能&#xff0c;包括但不限于 PNG、JPEG、BMP 等。QImageWriter 可以将图像写入文件&#xff0c;也可以写入任何 QIODevice&#xff0c;如 QByteArray&#xff0c;这使得它非常灵活…

pytorch简单神经网络模型训练

目录 一、导入包 二、数据预处理 三、定义神经网络 四、训练模型和测试模型 五、程序入口 一、导入包 import torch import torch.nn as nn import torch.optim as optim # 导入优化器 from torchvision import datasets, transforms # 导入数据集和数据预处理库 from tor…

python中type,object,class 三者关系

type,object,class 三者关系 在python中&#xff0c;所有类的创建关系遵循&#xff1a; type -> int -> 1 type -> class -> obj例如&#xff1a; a 1 b "abc" print(type(1)) # <class int> 返回对象的类型 print(type(int)) …

基于OpenCv的图像金字塔

⚠申明&#xff1a; 未经许可&#xff0c;禁止以任何形式转载&#xff0c;若要引用&#xff0c;请标注链接地址。 全文共计3077字&#xff0c;阅读大概需要3分钟 &#x1f308;更多学习内容&#xff0c; 欢迎&#x1f44f;关注&#x1f440;【文末】我的个人微信公众号&#xf…

sql-server--索引

SQL Server提供了五种索引类型&#xff0c;它们分别是 唯一索引 主键索引 聚集索引 &#xff08; Clustered Index &#xff09; 非聚集索引 &#xff08; Nonclustered Index &#xff09; 视图索引 --tb_student表的sno列创建索引 create index ix_sno on tb_student(…

【讲解如何OpenCV入门】

&#x1f308;个人主页: 程序员不想敲代码啊 &#x1f3c6;CSDN优质创作者&#xff0c;CSDN实力新星&#xff0c;CSDN博客专家 &#x1f44d;点赞⭐评论⭐收藏 &#x1f91d;希望本文对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提出指正&#xff0c;让我们共…

需求规格说明书编制书(word原件)

1 范围 1.1 系统概述 1.2 文档概述 1.3 术语及缩略语 2 引用文档 3 需求 3.1 要求的状态和方式 3.2 系统能力需求 3.3 系统外部接口需求 3.3.1 管理接口 3.3.2 业务接口 3.4 系统内部接口需求 3.5 系统内部数据需求 3.6 适应性需求 3.7 安全性需求 3.8 保密性需…

SQL语句每日一练十四

1378. 使用唯一标识码替换员工ID 题目 Employees 表&#xff1a; ------------------------ | Column Name | Type | ------------------------ | id | int | | name | varchar | ------------------------ 在 SQL 中&#xff0c;id 是这张表的…

GiantPandaCV | FasterTransformer Decoding 源码分析(二)-Decoder框架介绍

本文来源公众号“GiantPandaCV”&#xff0c;仅用于学术分享&#xff0c;侵权删&#xff0c;干货满满。 原文链接&#xff1a;FasterTransformer Decoding 源码分析(二)-Decoder框架介绍 作者丨进击的Killua 来源丨https://zhuanlan.zhihu.com/p/669303360 编辑丨GiantPand…

【Python编程实践1/3】模块

目录 目标 模块 import ​编辑 代码小结 题目 from...import 随机模块 代码小结 randint函数 骰子大战 choice函数 总结 目标 拧一颗螺丝&#xff0c;只会用到螺丝刀&#xff1b;但是修一台汽车&#xff0c;需要一整套汽修的工具。函数就像螺丝刀&#xff0c;可以帮…

寻找自己的25号底片

今天没啥知识干货昂&#xff0c;就单纯想跟诸君们聊聊题外话。这两天有些事儿挺忙的&#xff0c;所以呢也没时间为大家整理知识&#xff0c;这段时间一直把自己逼得挺紧的&#xff0c;所以也趁这两天给自己放个假&#xff0c;好好休息休息&#xff0c;还望诸君理解。 这两天那…

python项目==一个web项目,配置模板指定文件清洗规则,调用模板规则清洗文件

代码地址 一个小工具。 一个web项目&#xff0c;配置模板指定文件清洗规则&#xff0c;调用模板规则清洗文件 https://github.com/hebian1994/csv-transfer-all 技术栈&#xff1a; SQLite python flask vue3 elementplus 功能介绍&#xff1a; A WEB tool for cleaning…

JavaScript:Web APIs(三)

本篇文章的内容包括&#xff1a; 一&#xff0c;事件流 二&#xff0c;移除事件监听 三&#xff0c;其他事件 四&#xff0c;元素尺寸与位置 一&#xff0c;事件流 事件流是什么呢&#xff1f; 事件流是指事件执行过程中的流动路径。 我们发现&#xff0c;一个完整的事件执行…

AWTK 和 QT 资源占用不完全对比

因为没有开发两个完全一样的应用程序&#xff0c;对比的结果并不是很准确&#xff0c;仅供参考。 对比的程序为&#xff1a; AWTK demoui 演示了 AWTK 常用功能。 QT QDesktop 演示了 QT 常用功能。 运行平台为&#xff1a; i.MX6ULL Linux 1. 可以执行文件大小 1.1 AWTK…

Delta lake with Java--利用spark sql操作数据1

今天要解决的问题是如何使用spark sql 建表&#xff0c;插入数据以及查询数据 1、建立一个类叫 DeltaLakeWithSparkSql1&#xff0c;具体代码如下&#xff0c;例子参考Delta Lake Up & Running第3章内容 import org.apache.spark.sql.SaveMode; import org.apache.spark.…

【JavaScript】let,const 和 var 的区别

作用域&#xff1a; var 声明的变量具有全局作用域和函数作用域&#xff0c;可以跨块访问。let 和 const 声明的变量还具有块级作用域&#xff0c;意味着它们在声明它们的块&#xff08;例如&#xff0c;if 块、for 块、函数块等&#xff09;内可见。&#xff08;之前没有块作用…

区域文本提示的实时文本到图像生成;通过一致性自注意力机制的视频生成工具保持视频的一致性;专门为雪佛兰汽车设计的客服聊天机器人

✨ 1: StreamMultiDiffusion StreamMultiDiffusion是首个基于区域文本提示的实时文本到图像生成框架&#xff0c;实现了高速且互动的图像生成。 StreamMultiDiffusion 旨在结合加速推理技术和基于区域的文本提示控制&#xff0c;以克服之前解决方案中存在的速度慢和用户交互性…

约瑟夫问题新解法

前言 又碰到了约瑟夫问题&#xff0c;这样的题目本来用环形链表模拟的话就能做出来。然而&#xff0c;最近新学习了一种做法&#xff0c;实在是有点震惊到我了。无论是思路上&#xff0c;还是代码量上&#xff0c;都是那么的精彩。就想也震惊一下其他人。谁能想到原来模拟出来四…

C/C++程序设计实验报告综合作业 | 小小计算器

本文整理自博主本科大一《C/C程序设计》专业课的课内实验报告&#xff0c;适合C语言初学者们学习、练习。 编译器&#xff1a;gcc 10.3.0 ---- 注&#xff1a; 1.虽然课程名为C程序设计&#xff0c;但实际上当时校内该课的内容大部分其实都是C语言&#xff0c;C的元素最多可能只…