C语言刷二叉树(一)基础部分
二叉树基础部分
144. 二叉树的前序遍历
方法一:递归
/**
* 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().
*/ void PreTra(struct TreeNode* curNode, int *arr, int *len)
{
if (curNode == NULL) {
return;
}
// 前序:中 左 右
arr[(*len)++] = curNode->val;
PreTra(curNode->left, arr, len);
PreTra(curNode->right, arr, len);
} int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
*returnSize = 0;
int *res = (int *)malloc(sizeof(int) * 101);
PreTra(root, res, returnSize);
return res;
}
方法二:迭代(利用栈)
/**
* 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().
*/
int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
struct TreeNode *stack[10];
int stackIndex = 0;
if (root != NULL) {
stack[stackIndex++] = root;
}
int *res = (int *)malloc(sizeof(int) * 101);
*returnSize = 0;
while (stackIndex != 0) {
// 取出当前节点
struct TreeNode *curNode = stack[stackIndex - 1];
if (curNode == NULL) {
break;
}
stackIndex--;
res[(*returnSize)++] = curNode->val;
// 先右节点
if (curNode->right != NULL) {
stack[stackIndex++] = curNode->right;
}
// 再左节点
if (curNode->left != NULL) {
stack[stackIndex++] = curNode->left;
}
}
return res;
}
94. 二叉树的中序遍历
方法1:递归
/**
* 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().
*/ void InTra(struct TreeNode *curNode, int *arr, int *index)
{
if (curNode == NULL) {
return;
}
// 中序:左 中 右
InTra(curNode->left, arr, index);
arr[(*index)++] = curNode->val;
InTra(curNode->right, arr, index);
} int* inorderTraversal(struct TreeNode* root, int* returnSize)
{
int *res = (int *)malloc(sizeof(int) * 101);
*returnSize = 0;
InTra(root, res, returnSize);
return res;
}
方法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().
*/
int* inorderTraversal(struct TreeNode* root, int* returnSize)
{
struct TreeNode *stack[10];
int index = 0; int *res = (int *)malloc(sizeof(int) * 101);
*returnSize = 0;
struct TreeNode *curNode = root; while (curNode != NULL || index != 0) {
// 到最底层叶子节点,去取左孩子
if (curNode != NULL) {
stack[index++] = curNode;
curNode = curNode->left;
} else { // 否则开始弹栈1次
curNode = stack[--index];
res[(*returnSize)++] = curNode->val;
curNode = curNode->right;
}
}
return res;
}
145. 二叉树的后序遍历
方法1:递归
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ void PosTra(struct TreeNode *root, int *array, int *returnSize)
{
if (root == NULL) {
return;
}
PosTra(root->left, array, returnSize);
PosTra(root->right, array, returnSize);
array[(*returnSize)++] = root->val;
} /**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* postorderTraversal(struct TreeNode* root, int* returnSize)
{
int *res = (int*)malloc(sizeof(int) * 100);
*returnSize = 0;
PosTra(root, res, returnSize);
return res;
}
方法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().
*/ void Swap(int *a, int *b)
{
int tmp = *b;
*b = *a;
*a = tmp;
} int* postorderTraversal(struct TreeNode* root, int* returnSize)
{
struct TreeNode *stack[10];
int index = 0; if (root != NULL) {
stack[index++] = root;
} int *res = (int *)malloc(sizeof(int) * 101);
*returnSize = 0; while (index != 0) {
struct TreeNode *curNode = stack[--index];
res[(*returnSize)++] = curNode->val; if (curNode->left != NULL) {
stack[index++] = curNode->left;
}
if (curNode->right != NULL) {
stack[index++] = curNode->right;
} } // 翻转
int left = 0, right = *returnSize - 1; while (left < right) {
Swap(&res[left], &res[right]);
left++;
right--;
} return res;
}
C语言刷二叉树(一)基础部分的更多相关文章
- C语言刷二叉树(二)基础部分
102. 二叉树的层序遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeN ...
- 无刷电调基础知识以及BLHeli固件烧录和参数调整
标题: 无刷电调基础知识以及BLHeli固件烧录和参数调整 作者: 梦幻之心星 sky-seeker@qq.com 标签: [#基础知识,#电调,#BLHeli,#固件,#烧录,#调参] 目录: [电 ...
- C语言实现二叉树-02版
---恢复内容开始--- 昨天,提交完我们的二叉树项目后,今天早上项目经理早早给我打电话: 他说,小伙子干的不错.但是为什么你上面的insert是recusive的呢? 你难道不知道万一数据量大啦!那 ...
- C语言实现二叉树-利用二叉树统计单词数目
昨天刚参加了腾讯2015年在线模拟考: 四道大题的第一题就是单词统计程序的设计思想: 为了记住这一天,我打算今天通过代码实现一下: 我将用到的核心数据结构是二叉树: (要是想了解简单二叉树的实现,可以 ...
- 《C#语言和数据库技术基础》单词必备
<C#语言和数据库技术基础> 第一章1..NET Framework 框架2.sharp 尖锐,强烈的3.application 应用程序4.devel ...
- 快看Sample代码,速学Swift语言(2)-基础介绍 快看Sample代码,速学Swift语言(1)-语法速览
快看Sample代码,速学Swift语言(2)-基础介绍 Swift语言是一个新的编程语言,用于iOS, macOS, watchOS, 和 tvOS的开发,不过Swift很多部分内容,我们可以从C或 ...
- C语言的10大基础算法
C语言的10大基础算法 算法是一个程序和软件的灵魂,作为一名优秀的程序员,只有对一些基础的算法有着全面的掌握,才会在设计程序和编写代码的过程中显得得心应手.本文包括了经典的Fibonacci数列.简易 ...
- Leecode刷题之旅-C语言/python-111二叉树的最小深度
/* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minim ...
- Leecode刷题之旅-C语言/python-104二叉树最大深度
/* * @lc app=leetcode.cn id=104 lang=c * * [104] 二叉树的最大深度 * * https://leetcode-cn.com/problems/maxim ...
随机推荐
- 一:linux安装nginx
目录 1.yun安装 2.二进制安装 3.编译安装 1.yun安装 nginx官网:https://nginx.org/ [root@web01 ~]# vim /etc/yum.repos.d/ng ...
- 学习JAVAWEB第十二天
## Servlet: 1. 概念 2. 步骤 3. 执行原理 4. 生命周期 5. Servlet3.0 注解配置 6. Servlet的体系结构 Servlet -- 接口 | GenericSe ...
- AT2347 [ARC070C] NarrowRectangles
首先不难看出一个暴力的 \(dp\) 解法,考虑令 \(dp_{i, j}\) 表示考虑完前 \(i\) 个矩形,第 \(i\) 个矩形左端点在 \(j\) 时所需要的最小花费. 不难有转移: \[d ...
- JDK安装步骤
安装过程: 新建文件夹 新建文件夹 首先新建两个路径:D:\java\jdk和D:\java\jre,代表我把Java安装到D盘下的java路径下,在该路径下要新建两个路径,一会儿放jdk和jre. ...
- JAVA异常与异常处理详解【转】
感谢!!!原文地址:https://www.cnblogs.com/knightsu/p/7114914.html 一.异常简介 什么是异常? 异常就是有异于常态,和正常情况不一样,有错误出错.在ja ...
- 生成"cmd.exe"错误
转载请注明来源:https://www.cnblogs.com/hookjc/ 在VC2005里打开"工具"菜单,选择"选项",打开"选项" ...
- VC中如何将资源打包并释放到指定文件夹
转载请注明来源:https://www.cnblogs.com/hookjc/ 很多时候,我们可能要将某些文件打包到资源中,然后当程序执行的时候,发现缺少某些文件时,进行自我修复,以维持程序的正常执行 ...
- @property基本概念
1.什么是@property @property是编译器的指令 什么是编译器的指令 ? 编译器指令就是用来告诉编译器要做什么! @property会让编译器做什么呢? @property 用在声明文件 ...
- Nodejs允许跨域访问
状况:本地的前端项目(uni-app)以及后台管理(vue-mongo-node)和本地mongo数据库 前台项目端口是8082,后台数据接口是8081. 跨域解决,直接上代码: uni-app的ma ...
- if循环&数据类型的内置方法(上)
目录 if循环&数据类型的内置方法 for循环 range关键字 for+break for+continue for+else for循环的嵌套使用 数据类型的内置方法 if循环&数 ...