当前位置:网站首页>Brush the title "sword finger offer" day02
Brush the title "sword finger offer" day02
2022-07-25 08:05:00 【Pac Man programming】
Title source : Power button 《 The finger of the sword Offer》 The second edition
Completion time :2022/07/23
List of articles
07. Reconstruction of binary tree

My explanation
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(preorder.size() == 0){
return nullptr;
}
TreeNode* root = new TreeNode(preorder[0]);
int index = 0;
for(int i = 0;i < inorder.size();i++) {
if(inorder[i] == root->val){
index = i;
break;
}
}
preorder.erase(preorder.begin());
vector<int> leftPreorder(preorder.begin(),preorder.begin() + index);
vector<int> rightPreorder(preorder.begin() + index,preorder.end());
vector<int> leftInorder(inorder.begin(),inorder.begin() + index);
vector<int> rightInorder(inorder.begin() + index + 1,inorder.end());
root->left = buildTree(leftPreorder,leftInorder);
root->right = buildTree(rightPreorder,rightInorder);
return root;
}
};
09. Queues are implemented with two stacks

My explanation
class CQueue {
public:
stack<int> que1;
stack<int> que2;
CQueue() {
}
void appendTail(int value) {
que1.push(value);
}
int deleteHead() {
int result = -1;
while(que1.size() != 0){
que2.push(que1.top());
if(que1.size() == 1){
result = que1.top();
que2.pop();
}
que1.pop();
}
while(que2.size() != 0){
que1.push(que2.top());
que2.pop();
}
return result;
}
};
/** * Your CQueue object will be instantiated and called as such: * CQueue* obj = new CQueue(); * obj->appendTail(value); * int param_2 = obj->deleteHead(); */
边栏推荐
- 一款强大的端口扫描工具(nmap)
- mysql 如何获取两个集合的交集/差集/并集
- Teach you to use cann to convert photos into cartoon style
- Dijkstra sequence (summer vacation daily question 5)
- P1048 [NOIP2005 普及组 T3] 采药
- Advanced C language (11) - user defined data types
- When deep learning makes data sets, it specifies how many frames to extract an image from the long video to the specified file path
- 滴滴 - dispatching
- 查看电脑重启次数、原因
- P1048 [noip2005 popularization group t3] drug collection
猜你喜欢

Advanced C language (11) - user defined data types

Redis core principles

How to create a simple electron desktop program

滴滴 - dispatching

The two Nobel Prize winners became the chief scientist of the sky high price Baijiu of "taishanglaojun holding a dream"

Check the computer restart times and reasons

【音视频】图片YUV数据格式

Supplementary notes on Relevant Issues of complete model group

Programmers can't play at the age of 35. Is it a fact or a misunderstanding?

Vscode remote connection server, switch to go version
随机推荐
P1046 [noip2005 popularity group t1] Taotao picking apples
[QNX Hypervisor 2.2用户手册]9.3 cpu
C# 43. 获取UDP可用端口
7.24 simulation summary
How does MTK change the boot logo?
Learn no when playing 8 | the enterprise saves hundreds of thousands in an instant, just because it uses it
Common commands of raspberry pie
机器学习入门详解(一):理解监督学习中的最大似然估计
How to do a good job in safety development?
"Unable to recognize" yarn "item as cmdlet, function, script file
enq: HW – contention等待引起的故障分析
Dijkstra序列(暑假每日一题 5)
Advanced C language (XIII) - Example Analysis of dynamic memory management
交叉熵计算公式
node+js搭建时间服务器
对无法重新创建的表进行了更改或者启用了“阻止保存要求重新创建表的更改”选项
How to reverse a stack with recursive functions and stack operations only
IoT物联网嵌入式设备中30种常见传感器模块简介及原理讲解
How should enterprise users choose aiops or APM?
Test the mock data method of knowing and knowing