# 【代码随想录】【算法训练营】【第15天】 [102]二叉树的层序遍历 [226]翻转二叉树 [101]对称二叉树

42 篇文章 2 订阅

## 前言

day 15，一周中最困难的周三~

## 题目详情

### [102] 二叉树的层序遍历

#### 代码实现

##### C语言
/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     struct TreeNode *left;
*     struct TreeNode *right;
* };
*/
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int** levelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes) {
//输出结果初始化
*returnSize = 0;

// 空结点
if (root == NULL)
{
return NULL;
}

int **ret = NULL;
struct TreeNode *queue[2000];
int idx = 0;
int start = 0;
queue[idx++] = root;
while (start < idx)
{
// 动态分配输出数组大小
(*returnSize)++;
ret = (int **)realloc(ret, sizeof(int *) * (*returnSize));
*returnColumnSizes = (int *)realloc(*returnColumnSizes, sizeof(int) * (*returnSize));
(*returnColumnSizes)[(*returnSize) - 1] = idx - start;
ret[(*returnSize) - 1] = (int *)malloc(sizeof(int) * ((*returnColumnSizes)[(*returnSize) - 1]));
// 输出该层结点
for (int i = 0; i < (*returnColumnSizes)[(*returnSize) - 1]; i++)
{
struct TreeNode *cur = queue[start++];
// 左结点压栈
if (cur->left)
{
queue[idx++] = cur->left;
}
// 右结点压栈
if (cur->right)
{
queue[idx++] = cur->right;
}
// 输出该结点
ret[(*returnSize) - 1][i] = cur->val;
}
}
return ret;
}


### [226] 翻转二叉树

#### 代码实现

##### C语言
###### 先序遍历 递归
/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     struct TreeNode *left;
*     struct TreeNode *right;
* };
*/
void reversalTree(struct TreeNode *root)
{
if (root == NULL)
{
return ;
}
// 左右结点反转
struct TreeNode *tmp = root->left;
root->left = root->right;
root->right = tmp;
reversalTree(root->left);
reversalTree(root->right);
return ;
}

struct TreeNode* invertTree(struct TreeNode* root) {
reversalTree(root);
return root;
}

###### 先序遍历 迭代
/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     struct TreeNode *left;
*     struct TreeNode *right;
* };
*/
struct TreeNode* invertTree(struct TreeNode* root) {
struct TreeNode *stack[100];
int idx = 0;
if (root != NULL)
{
stack[idx++] = root;
}
while (idx > 0)
{

struct TreeNode *cur = stack[--idx];
// 左右结点非空入栈
if (cur->left)
{
stack[idx++] = cur->left;
}
if (cur->right)
{
stack[idx++] = cur->right;
}
// 交换左右结点
struct TreeNode *tmp = cur->left;
cur->left = cur->right;
cur->right = tmp;
}
return root;
}


### [101] 对称二叉树

#### 代码实现

##### C语言
###### 先序遍历 递归
/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     struct TreeNode *left;
*     struct TreeNode *right;
* };
*/

bool isSymmetricFound(struct TreeNode* left, struct TreeNode* right)
{
if ((left == NULL) && (right == NULL))
{
return true;
}
else if (((left != NULL) && (right == NULL)) || ((left == NULL) && (right != NULL)))
{
return false;
}
else if (left->val != right->val)
{
return false;
}
// 递归
bool ans = false;
ans = isSymmetricFound(left->left, right->right);
if (ans != true)
{
return false;
}
return isSymmetricFound(left->right, right->left);
}

bool isSymmetric(struct TreeNode* root) {
// 判断空树
if (root == NULL)
{
return true;
}
return isSymmetricFound(root->left, root->right);
}

###### 先序遍历 迭代
/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     struct TreeNode *left;
*     struct TreeNode *right;
* };
*/
bool isSymmetric(struct TreeNode* root) {
// 判断空树
if (root == NULL)
{
return true;
}
// 使用栈(队列同样的处理流程)
struct TreeNode *stack[1000];
int idx = 0;
// left right成对入栈，即使为null
stack[idx++] = root->right;
stack[idx++] = root->left;
while (idx)
{
struct TreeNode *left = stack[--idx];
struct TreeNode *right = stack[--idx];
if ((left == NULL) && (right == NULL))
{
continue;
}
else if (((left != NULL) && (right == NULL)) || ((left == NULL) && (right != NULL)))
{
return false;
}
else if (left->val != right->val)
{
return false;
}
// 此时左右结点均为非空，将该左右结点的子节点 成对 入栈
stack[idx++] = right->right;
stack[idx++] = left->left;
stack[idx++] = left->right;
stack[idx++] = right->left;
}
return true;
}


## 今日收获

1. 二叉树的遍历，递归及迭代的实现。
• 4
点赞
• 10
收藏
觉得还不错? 一键收藏
• 0
评论
09-22
01-11 422
11-08 2009
03-20 611
12-13 63
01-25 599
05-04 990
06-21 392
06-23 289

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。