二叉树基础部分

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. python全局解释器GIL锁(-死锁)

    目录 一:Python中的GIL锁 1.GIL介绍 2.GIL的作用 3.cpython 4.内存管理>>>垃圾回收机制 二:全局解释器锁GIL 1.GIL特点 三:计算密集型与IO ...

  2. K8s PV and PVC and StorageClass

    PVC和PV之间没有依靠ID.名称或者label匹配,而是靠容量和访问模式,PVC的容量和访问模式需要是某个PV的子集才能自动匹配上.注意:PVC和PV是一对一的,也即一个PV被一个PVC自动匹配后, ...

  3. Ubuntu更换镜像源

    不同的源 当修改sources.list文件时,我们需要将下面任意一个镜像源的代码复制粘贴到该文件中. 阿里源 # 阿里镜像源 deb http://mirrors.aliyun.com/ubuntu ...

  4. markdown介绍和使用(超全建议收藏)

    Markdown介绍 Markdown 其实在 2004 年就有了,不过之前一直很小众,这几年随着相关应用平台的发展,Markdown以其独到的优势迅速火起来了.Markdown编辑器使用一套格式标记 ...

  5. c#开方,平方,sin函数计算

    平方运算 private double m; private double n=Math.Pow(m,2.0); 开平方运算 System.Math.Sqrt(数字); double保留两位小数 Ma ...

  6. 「CTSC2010」产品销售

    「CTSC2010」产品销售 30pts的费用流都会吧... 100pts只要模拟费用流就行了,是不是很简单呀( 咕咕咕 令\(M_i\)表示\(i-1\to i\)的正向边,\(M_i^{'}\)表 ...

  7. 交换机基本原理与VRP基础及操作

    交换机基本原理与VRP基础及操作 目录 交换机基本原理与VRP基础及操作 一.数据链路层 1.数据链路层的位置 2.数据链路层的功能 二.以太网(Ethernet) 1.以太网的概念 2.MAC地址( ...

  8. Shell数组以及排序算法(冒泡、直接选择、反转)

    Shell数组以及排序算法(冒泡.直接选择.反转) 目录 Shell数组以及排序算法(冒泡.直接选择.反转) 一.数组概述 1. 数组的定义 2. 下标的定义 3. 数组的特点 4. 数组定义的方法 ...

  9. 「2022」打算跳槽涨薪,必问面试题及答案 -- ECMAScript 篇

    起点低怕什么,大不了加倍努力.人生就像一场马拉松比赛,拼的不是起点,而是坚持的耐力和成长的速度.只要努力不止,进步也会不止. 1.ECMAScript 与 JavaScript 的关系? ECMA(E ...

  10. JS 获取JSON返回的时间值转换为通常格式展示

    var date = new Date(parseInt(数据源.slice(6)));   //获取到时间  年月日时分秒 var result = date.getFullYear() + '/' ...