当前位置:网站首页>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(); */
边栏推荐
- Programmers can't play at the age of 35. Is it a fact or a misunderstanding?
- Science: listening to music can really relieve pain. Chinese scientists reveal the neural mechanism behind it
- Raspberrypico analytic PWM
- 文件详细操作
- yolov7 网络架构深度解析
- Learn when playing No 2 | learning is too boring? Learning maps lets you learn and play
- Learn when playing No 3 | are enterprise learning resources wasted? Learn map one trick to solve!
- One of C language multithreading programming
- Learn when playing No 4 | can you take an online exam in programming? Right here →
- efcore在Saas系统下多租户零脚本分表分库读写分离解决方案
猜你喜欢

Network file storage system (III) practice of fastdfs distributed file system

"Unable to recognize" yarn "item as cmdlet, function, script file

如何仅用递归函数和栈操作逆序一个栈
![[unity introduction program] basic concepts GameObject & components](/img/fc/7e0a6f057394a6fd4409a7bf518ba5.png)
[unity introduction program] basic concepts GameObject & components

ArcGIS Pro脚本工具(10)——从图层生成.stylx样式符号

Raspberry pie 4B parsing PWM

Test the mock data method of knowing and knowing

Oracle trigger creation

CM4 development cross compilation tool chain production
![[unity entry program] basic concept trigger](/img/16/cd0f8ae579627fc095935195136729.png)
[unity entry program] basic concept trigger
随机推荐
How to do a good job in safety development?
MathWorks has been in China for 15 years. What are the secrets of continuous innovation?
475-82(230、43、78、79、213、198、1143)
Didi eta (estimate the travel time)
Programmers can't play at the age of 35. Is it a fact or a misunderstanding?
P1049 [noip2001 popularization group t4] packing problem
如何仅用递归函数和栈操作逆序一个栈
[unity introduction program] basic concepts - 2D collider collider 2D
A review of nature: gender differences in anxiety and depression - circuits and mechanisms
Raspberry pie creation self start service
Vscode remote connection server, switch to go version
CSDN custom T-shirts are waiting for you to get, and the benefits of new programmer are coming!
while(~scanf(“%d“, &n)) 等价于 while(scanf(“%d“,&n)!=EOF)
Learn when playing No 3 | are enterprise learning resources wasted? Learn map one trick to solve!
How does MTK change the boot logo?
刷题《剑指Offer》day02
pom容易忽略的问题
The two Nobel Prize winners became the chief scientist of the sky high price Baijiu of "taishanglaojun holding a dream"
Redis core principles
Network file storage system (II) practical operation of Minio distributed file system