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 ...
随机推荐
- 源码分析axios(1)~源码分析、模拟axios的创建
■ 查看源码发现,起初axios[instance=bind(Axios.prototype.request, context);]是一个函数, 但后续[ utils.extend(instance, ...
- 基于Redis&MySQL接口幂等性设计
基于Redis&MySQL接口幂等性设计 欲把相思说似谁,浅情人不知. 1.幂等 幂等性即多次调用接口或方法不会改变业务状态,可以保证重复调用的结果和单次调用的结果一致. 2.幂等使用场景 前 ...
- 微服务架构 | 10.1 使用 Sleuth 追踪服务调用链
目录 前言 1. Sleuth 基础知识 1.1 Sleuth 原理 2. 在服务中使用 Sleuth 追踪 2.1 引入 pom.xml 依赖文件 2.2 查看日志信息 最后 前言 参考资料: &l ...
- 回顾 2021 中国 .NET 开发者峰会
.NET Conf China 2021 是面向开发人员的社区峰会,基于 .NET Conf 2021,庆祝 .NET 6 的发布和回顾过去一年来 .NET 在中国的发展.峰会由来自北京.上海.苏州. ...
- Core Animation的使用步骤
- 诗和远方-target
学习也是这样:不以结婚为目的的谈恋爱,都是耍流氓!
- UIKit坐标系
在UIKit中,坐标系的原点(0,0)在左上角,x值向右正向延伸,y值向下正向延伸
- DbUnit入门实战
原文地址: http://yangzb.iteye.com/blog/947292 相信做过单元测试的人都会对JUnit 非常的熟悉了, 今天要介绍的DbUnit(http://dbunit.sour ...
- iOS,开发准备之申请证书 ---by吴帮雷
一.申请真机调试证书 打开iOS Dev Center,选择Sign in,登陆(至少99美元账号),登陆选择Certificates,Identifiers & Profiles --> ...
- HDOJ 1249 三角形『平面分隔』
很水拉 为了记规律- - 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1249 分隔平面公式 下面是我自己查找的公式,没有推到过程,但可以给一些链 ...