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 ...
随机推荐
- Python与Javascript相互调用超详细讲解(四)使用PyNode进行Python与Node.js相互调用项(cai)目(keng)实(jing)践(yan)
目录 前提 安装 使用 const pynode = require('@fridgerator/pynode')的时候动态链接错误 ImportError: math.cpython-39-x86_ ...
- Azure AD Domain Service(一)将 Azure VM 实例添加到域服务里
一,引言 有网友提到实际项目中如何将 Azuer VM 实例加入到 Azure AD 域,那我们今天就带着整个问题开始今天的分析!首先我们得了解什么是 Azure AD 域服务,Azure AD 域服 ...
- JVM诊断及工具笔记(2)使用arthas定位哪里执行了System#gc()
笔者是汽车之家实时计算平台的一名小伙伴.负责flink平台,数据湖及kafka平台的设计与开发.平时擅长做平台设计,定位及解决各种疑难杂症.第二篇文章,讲的点依旧很小,但是这次图多!!! 在这里感谢支 ...
- Spring Security过滤器链体系
以下摘自胖哥分享的 2022开工福利教程. 在学习Spring Security的时候你有没有下面这两个疑问: Spring Security的登录是怎么配置的? Spring Security的访问 ...
- SpringBoot前后端数组交互
前端 后端 Gitee地址 https://gitee.com/zhuayng/foundation-study.git 参考 https://blog.csdn.net/qq_34091758/ar ...
- Mysql Json函数之更新 (四)
修改JSON值的函数 本节中的函数将修改JSON值并返回结果. JSON_APPEND(json_doc, path, val[, path, val] ...) 将值附加到JSON文档中指定数组的末 ...
- synchronize类锁用父类作为锁能否锁住代码块
如果有一个父类,Demo如下 public class Demo { public void demo1(){ synchronized (Demo.class){ while (true){ Sys ...
- Solution -「多校联训」染色
\(\mathcal{Description}\) Link. 给定 \(n\) 和 \(q\) 次询问,每次询问给出 \(x,k\),求第 \(x\) 位为 0 且任意两个 1 的下标之差不 ...
- Solution -「AGC 012F」「AT 2366」Prefix Median
\(\mathcal{Description}\) Link. 给定序列 \(\{a_{2n-1}\}\),将 \(\{a_{2n-1}\}\) 按任意顺序排列后,令序列 \(b_i\) 为前 ...
- Solution -「NOI 2018」「洛谷 P4768」归程
\(\mathcal{Description}\) Link. 给定一个 \(n\) 个点 \(m\) 条边的无向连通图,边形如 \((u,v,l,a)\).每次询问给出 \(u,p\),回答 ...