c语言刷 DFS题记录
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 Preorder(struct TreeNode* root, int* array, int* returnSize)
{
if (root == NULL) {
return;
}
array[(*returnSize)++] = root->val;
Preorder(root->left, array, returnSize);
Preorder(root->right, array, returnSize);
} int* preorderTraversal(struct TreeNode* root, int* returnSize)
{
int *res = (int *)malloc(sizeof(int) * 100);
*returnSize = 0;
Preorder(root, res, returnSize);
return res;
}
145. 二叉树的后序遍历
/**
* 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 Postorder(struct TreeNode* root, int* array, int* returnSize)
{
if (root == NULL) {
return;
}
Postorder(root->left, array, returnSize);
Postorder(root->right, array, returnSize);
array[(*returnSize)++] = root->val;
} int* postorderTraversal(struct TreeNode* root, int* returnSize)
{
int *res = (int *)malloc(sizeof(int) * 100);
*returnSize = 0;
Postorder(root, res, returnSize);
return res;
}
105. 从前序与中序遍历序列构造二叉树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize)
{
if (preorder == NULL || inorder == NULL) {
return NULL;
}
if (preorderSize == 0 || inorderSize == 0) {
return NULL;
}
if (preorderSize != inorderSize) {
return NULL;
} struct TreeNode *res = (struct TreeNode *)malloc(sizeof(struct TreeNode));
int rootVal = preorder[0];
int leftLen = 0;
for (int i = 0; i < inorderSize; i++) {
if (rootVal == inorder[i]) {
leftLen = i;
break;
}
}
int rightLen = inorderSize - leftLen - 1; res->val = rootVal;
res->left = buildTree(&preorder[1], leftLen, inorder, leftLen);
res->right = buildTree(&preorder[leftLen + 1], rightLen, &inorder[leftLen + 1], rightLen);
return res;
}
106. 从中序与后序遍历序列构造二叉树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/ struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize)
{
if (inorder == NULL || postorder == NULL) {
return NULL;
}
if (inorderSize == 0 || postorderSize == 0) {
return NULL;
}
if (inorderSize != postorderSize) {
return NULL;
} struct TreeNode* res = (struct TreeNode *)malloc(sizeof(struct TreeNode));
int rootVal = postorder[postorderSize - 1];
int leftNum = 0;
int rightNum = 0;
for (int i = 0; i < inorderSize; i++) {
if (rootVal == inorder[i]) {
leftNum = i;
break;
}
}
rightNum = inorderSize - leftNum - 1;
res->val = rootVal;
res->left = buildTree(inorder, leftNum, postorder, leftNum);
res->right = buildTree(&inorder[leftNum + 1], rightNum, &postorder[leftNum], rightNum);
return res;
}
c语言刷 DFS题记录的更多相关文章
- C语言刷数组题记录
讲解:https://mp.weixin.qq.com/s/weyitJcVHBgFtSc19cbPdw 二分法: 704. 二分查找 int search(int* nums, int numsSi ...
- c语言刷 队列题记录
622. 设计循环队列 https://blog.csdn.net/Galaxy_n/article/details/115978544 typedef struct { int *arrs; int ...
- c语言刷 链表题记录
61. 旋转链表 /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode * ...
- 用PHP语言刷OJ题
平常在学校都是用C,C++,Java来刷OJ题,把AC的题用不同的语言再AC一次,基本相当于翻译而已.看到学校的OJ支持提交PHP代码,于是尝试了一下. 首先,得会使用PHP,但是你如果在看这篇博客, ...
- 8.20~8.25刷散题记录 By cellur925
记录一些散题 / 价值不大但还是想记下来的题目 / 没正八经写博客的题目 8.24 Luogu P1508 沙雕题数字三角形的二维升级版,但是注意阅读理解,李大水牛从桌子最后一行下侧开始吃,而本题是自 ...
- c语言刷 设计题合计
355. 设计推特 #define MAX_LEN 512 struct User { int userId; int followee[MAX_LEN]; // 散列表,0/1,1表示这个user被 ...
- PE刷题记录
PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...
- SPOJ 刷题记录
按点赞数降序 297 二分 #include<bits/stdc++.h> using namespace std; #define fi first #define se second ...
- 刷题记录:[SUCTF 2019]CheckIn
目录 刷题记录:[SUCTF 2019]CheckIn 一.涉及知识点 1.利用.user.ini上传\隐藏后门 2.绕过exif_imagetype()的奇技淫巧 二.解题方法 刷题记录:[SUCT ...
随机推荐
- Codeforces Round #740 Div. 2
题目跳转链接 A. Simply Strange Sort 题意 定义一个函数\(f_{i}\) : 如果\(a_i \ge a_{i+1}\) swap(\(a_i\) \(a_{i+1}\)) 定 ...
- python程序语法元素分析
#TemConvert.py TempStr = input("请输入带有符号的温度值:") if TempStr[-1] in ['F', 'f']: C = (eval(Tem ...
- JQuery扩展方法实现Form表单与Json互相转换
1.把表单转换出json对象 //把表单转换出json对象 $.fn.toJson = function () { var self = this, json = {}, push_counters ...
- shell下快捷键
### 1.快捷键 ^C 终止前台运行的程序 ^D 退出 等价于exit ^L 清屏 ^A 光标移动到命令行的最前端 ^E 光标移动到命令行的最后端 ^U 删除光标前所有字符 ...
- js表达式和语句
表达式 一个表达式可以产生一个值,有可能是运算.函数调用.有可能是字面量.表达式可以放在任何需要值的地方. 语句 语句可以理解为一个行为,循环语句和判断语句就是典型的语句.一个程序有很多个语句组成,一 ...
- centOs7.2安装cmake
最新的3.15的安装不上 wget https://cmake.org/files/v3.5/cmake-3.5.2.tar.gz tar xvf cmake-3.5.2.tar.gz cd cmak ...
- rpm与yum安装及管理程序
安装及管理程序 1.Linux应用程序基础 2.RPM软件包管理工具 3.yum源仓库创建 1.应用程序与系统命令的关系如图: 典型应用程序的目录结构如图: 常见的软件包封装类型如图: 2.RPM包 ...
- Linux常用命令精华讲解 上部 (下部下回分解)不要催很忙的
Linux常用命令讲解 1.Linux命令基础 2.Linux命令帮助 3.目录与文件的基操 1.Shell是系统中运行的一种特殊程序在用户和内核之间充当"翻译官"的角色,登录li ...
- docker错误处理——docker Job for docker.service failed because the control process exited with error code.
(15条消息) docker Job for docker.service failed because the control process exited with error code._Hel ...
- Ubuntu18修改系统时间
1. 运行 tzselect 依次选择 Asia -> China -> Beijing Time 2. 复制文件到 /etc 下 sudo cp /usr/share/zoneinfo/ ...