二叉树基础部分

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语言刷二叉树(一)基础部分的更多相关文章

  1. C语言刷二叉树(二)基础部分

    102. 二叉树的层序遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeN ...

  2. 无刷电调基础知识以及BLHeli固件烧录和参数调整

    标题: 无刷电调基础知识以及BLHeli固件烧录和参数调整 作者: 梦幻之心星 sky-seeker@qq.com 标签: [#基础知识,#电调,#BLHeli,#固件,#烧录,#调参] 目录: [电 ...

  3. C语言实现二叉树-02版

    ---恢复内容开始--- 昨天,提交完我们的二叉树项目后,今天早上项目经理早早给我打电话: 他说,小伙子干的不错.但是为什么你上面的insert是recusive的呢? 你难道不知道万一数据量大啦!那 ...

  4. C语言实现二叉树-利用二叉树统计单词数目

    昨天刚参加了腾讯2015年在线模拟考: 四道大题的第一题就是单词统计程序的设计思想: 为了记住这一天,我打算今天通过代码实现一下: 我将用到的核心数据结构是二叉树: (要是想了解简单二叉树的实现,可以 ...

  5. 《C#语言和数据库技术基础》单词必备

    <C#语言和数据库技术基础> 第一章1..NET Framework   框架2.sharp            尖锐,强烈的3.application      应用程序4.devel ...

  6. 快看Sample代码,速学Swift语言(2)-基础介绍 快看Sample代码,速学Swift语言(1)-语法速览

    快看Sample代码,速学Swift语言(2)-基础介绍 Swift语言是一个新的编程语言,用于iOS, macOS, watchOS, 和 tvOS的开发,不过Swift很多部分内容,我们可以从C或 ...

  7. C语言的10大基础算法

    C语言的10大基础算法 算法是一个程序和软件的灵魂,作为一名优秀的程序员,只有对一些基础的算法有着全面的掌握,才会在设计程序和编写代码的过程中显得得心应手.本文包括了经典的Fibonacci数列.简易 ...

  8. Leecode刷题之旅-C语言/python-111二叉树的最小深度

    /* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minim ...

  9. Leecode刷题之旅-C语言/python-104二叉树最大深度

    /* * @lc app=leetcode.cn id=104 lang=c * * [104] 二叉树的最大深度 * * https://leetcode-cn.com/problems/maxim ...

随机推荐

  1. MySQL存储引擎(最全面的概括)

    目录 一:MySQL存储引擎 1.什么是存储引擎? 2.查看存储引擎信息 二:MySQL支持的存储引擎 1.存储引擎 三:innoDB存储引擎 1.特性 2.存储结构 3.优缺点.适用场景 四:MyI ...

  2. 在树莓派上开发SpringBoot 之使用VSCode远程开发

    一些运行在ARM单板电脑上的IoT应用通常会提供RESTful风格的API接口.本次的文章记录如何在本地电脑上通过VS Code的远程开发功能,在树莓派端创建一个SpringBoot工程,并实现调试和 ...

  3. python 小兵之小技巧

    用for循环打印数字从1开始 for a in range(1,num+1): 用split切割字符串可以用索引选择部分 int(el.split("_")[1]) range 第 ...

  4. POJ 1927 Area in Triangle 题解

    link Description 给出三角形三边长,给出绳长,问绳在三角形内能围成的最大面积.保证绳长 \(\le\) 三角形周长. Solution 首先我们得知道,三角形的内切圆半径就是三角形面积 ...

  5. Lesson8——Pandas reindex重置索引

    pandas目录 1 简介 重置索引(reindex)可以更改原 DataFrame 的行标签或列标签,并使更改后的行.列标签与 DataFrame 中的数据逐一匹配.通过重置索引操作,您可以完成对现 ...

  6. Git远程仓库地址操作

    添加 git remote add test1_origin git@github.com:b84955189/test1.git test1_origin:远程地址名,这里是我自定的. git@gi ...

  7. Java程序性能监控工具

    系统性能监控: 确定系统运行的整体状态,基本定位问题所在 uptime命令 [root@localhost ~]# uptime23:19:38 up 244 days, 3:39, 34 users ...

  8. LeetCode随缘刷题之无重复字符的最长子串

    欢迎评论区交流. package leetcode.day_12_04; /** * 给定一个字符串 s ,请你找出其中不含有重复字符的最长子串的长度. * <p> * 示例1: * &l ...

  9. 用Express 创建项目

    1.Node.js Express 框架安装:npm install express --save在当前目录下创建一个node_modules 2.安装必要的中间件npm install body-p ...

  10. [源码解析] NVIDIA HugeCTR,GPU版本参数服务器---(3)

    [源码解析] NVIDIA HugeCTR,GPU版本参数服务器---(3) 目录 [源码解析] NVIDIA HugeCTR,GPU版本参数服务器---(3) 0x00 摘要 0x01 回顾 0x0 ...