当前位置:网站首页>刷题《剑指Offer》day02
刷题《剑指Offer》day02
2022-07-25 08:01:00 【吃豆人编程】
题目来源:力扣《剑指Offer》第二版
完成时间:2022/07/23
07. 重建二叉树

我的题解
/** * 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. 用两个栈实现队列

我的题解
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(); */
边栏推荐
- P1086 [noip2004 popularization group question 2] peanut picking
- C 43. Get UDP available ports
- Programmers can't play at the age of 35. Is it a fact or a misunderstanding?
- 【FFmpeg】mp4转yuv
- Check the computer restart times and reasons
- [unity introduction plan] interface Introduction (2) -games view & hierarchy & Project & Inspector
- Enq: HW - fault analysis caused by content waiting
- 纳米数据足球数据,足球赛事比分,体育数据api,卡塔尔世界杯
- Open source, innovators win, 2022 "science and innovation China" open source innovation list selection is fully open!
- Supplementary notes on Relevant Issues of complete model group
猜你喜欢

交叉熵计算公式

Enq: HW - fault analysis caused by content waiting

VS2019 C# MFC安装

【Unity入门计划】基本概念-2D碰撞体Collider 2D
Technical Analysis | Doris connector combined with Flink CDC to achieve accurate access to MySQL database and table exactly once

Hikaricp connection pool does not operate for a period of time, and the data is automatically disconnected

【Unity入门计划】基本概念-预制件 Prefab

Today in history: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
![[unity entry program] make my first little game](/img/e7/5dcb113c7fabd73ed632fb29619369.png)
[unity entry program] make my first little game

【微信小程序】全局样式、局部样式、全局配置
随机推荐
CAS操作
Learn when playing No 3 | are enterprise learning resources wasted? Learn map one trick to solve!
The two Nobel Prize winners became the chief scientist of the sky high price Baijiu of "taishanglaojun holding a dream"
Teach you to use cann to convert photos into cartoon style
Hikaricp connection pool does not operate for a period of time, and the data is automatically disconnected
475-82(230、43、78、79、213、198、1143)
文件详细操作
Cerebral cortex: the relationship between lifestyle and brain function in the elderly and its relationship with cognitive decline
Summer Challenge harmonyos - slider slider for custom components
How to create a simple electron desktop program
Install homebrew, NVM and verdaccio to build a private NPM warehouse
Dijkstra序列(暑假每日一题 5)
Using one stack to sort another stack
Supplementary notes on Relevant Issues of complete model group
Sqlserver has opened the CDC table. Why can we only pull the full amount of data and can't recognize the incremental data
[audio and video] picture YUV data format
P1047 [noip2005 popularization group t2] tree outside the school gate
Is the yield of financial products high or low?
Learn when playing No 1 | can live classroom still be like this? Come and unlock "new posture"!
While (~scanf ("%d", & n)) is equivalent to while (scanf ("%d", & n)! =eof)