概述
目录
Day35-学习内容:
1.剑指Offer
面试题7:重建二叉树
面试题8:二叉树的下一个结点
2.Leetcode
例1:atoi函数实现-字符串转整数
例2:股票买卖的最大利润
1.剑指Offer
面试题7:重建二叉树
题目描述:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。
思路:递归
考点:前序和中序遍历
代码:
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
int vinlen=vin.size();
if(vinlen==0){
return NULL;
}
int rootval=pre[0];
int p=0;
for(p;p<vinlen;p++){
if(vin[p]==rootval){
break;
}
}
vector<int> preleft,preright,vinleft,vinright;
for(int i=0;i<vinlen;i++){
if(i<p){
vinleft.push_back(vin[i]);
preleft.push_back(pre[i+1]);
}
else if(i>p){
vinright.push_back(vin[i]);
preright.push_back(pre[i]);
}
}
TreeNode* root=new TreeNode(rootval);
root->left=reConstructBinaryTree(preleft,vinleft);
root->right=reConstructBinaryTree(preright,vinright);
return root;
}
};
面试题8:二叉树的下一个结点
题目描述:给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
思路:画图、举例
考点:中序遍历
代码:
class Solution {
public:
TreeLinkNode* GetNext(TreeLinkNode* pNode)
{
if(pNode==nullptr){
return nullptr;
}
TreeLinkNode* pNext=nullptr;
if(pNode->right!=nullptr){
TreeLinkNode* pRight=pNode->right;
while(pRight->left!=nullptr){
pRight=pRight->left;
}
pNext=pRight;
}
else if(pNode->next!=nullptr){
TreeLinkNode* pCurrent=pNode;
TreeLinkNode* pParent=pNode->next;
while(pParent!=nullptr&&pCurrent==pParent->right){
pCurrent=pParent;
pParent=pParent->next;
}
pNext=pParent;
}
return pNext;
}
};
2.Leetcode
例1:atoi函数实现-字符串转整数
题目描述:
Implement atoi to convert a string to an integer.
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.
spoilers alert... click to show requirements for atoi.
Requirements for atoi:
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.
思路:考虑多种特殊情况(1.去掉前面若干空字符 2.开头正负号 3.整数溢出 4.非数字字符)
代码:
class Solution {
public:
int atoi(const char *str) {
if(str==nullptr) return 0;
long long res=0;
int i=0;
while(str[i]==' '){
i++;
}
bool flag=false;
if(str[i]=='+'){
i++;
}
if(str[i]=='-'){
i++;
flag=true;
}
for(i;str[i]!='