二叉树基础部分

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. mongo用户认证

    mongo@rayos:/opt/mongodb$ mongo --port 28017 MongoDB shell version v4.0.13 connecting to: mongodb:// ...

  2. 昔日埋雷不经意,今朝踩雷排查难:JetBrains系列IDE使用SFTP连接远程服务器报“EOF while reading packet”解决方法

    写在前面 这是一篇问题解决记录.希望能帮到遇到同样问题的读者. 强烈建议:请您先看解决步骤一节,如果您发现在下的问题和您的问题不一样,就可以及时离开本文,避免浪费时间. 正文 问题描述 在使用GoLa ...

  3. 「NOI十联测」深邃

    「NOI十联测」深邃 要使得最大的连通块最小,显然先二分答案. 先固定1结点为根. 对于一个果实,显然是先处理子树中未分配的点,再向外延伸. 每个结点记录一个\(si[]\),表示子树中未分配的点数, ...

  4. js 实现光标控制与字符串查找

    转载请注明来源:https://www.cnblogs.com/hookjc/ 光标定位: <html> <head> <meta http-equiv="co ...

  5. WebSocket协议详解及应用

    WebSocket协议详解及应用(七)-WebSocket协议关闭帧 本篇介绍WebSocket协议的关闭帧,包括客户端及服务器如何发送并处理关闭帧.关闭帧错误码及错误处理方法.本篇内容主要翻译自RF ...

  6. position和anchorPoint笔记

    position和anchorPoint是CAlayer的两个属性.     我们以前修改一个控件的位置都是能过Frame的方式进行修改.     现在利用CALayer的position和ancho ...

  7. SqlServer数据库表生成C# Model实体类SQL语句——补充

    在sql语句最前边加上  use[数据库名] 原链接:https://www.cnblogs.com/jhli/p/11552105.html   --[SQL骚操作]SqlServer数据库表生成C ...

  8. 直播流媒体ums

    准备工具 下载  UltrantMediaServer服务器 FlashMediaLiveEncoder测试直播工具 第一步 安装 UltrantMediaServer服务器 第二步 打开网也输入   ...

  9. linux+nginx+tomcat负载均衡,实现session同步

    第一部分:nginx反向代理tomcat 一.软件及环境 软件 系统 角色 用途 安装的软件 ip地址 Centos6.5x86_64 nginx 反向代理用户请求 nginx 172.16.249. ...

  10. shell脚本之数组基本操作及排序

    数组的基本操作及排序 1.数组定义方法: ( 6 7 9 4 3 2) 0 1 2 3 4 5 #下标号 方法一: 数组名=(value0 value1 value2 -) 方法二: 数组名=([0] ...