難題:A1114、A1119
0、讓Dev C++支持c11
Tools->Compier Options->Add the following commands when calling the compiler:
選擇該選項卡,讓后添加:-std=c+11,即可
0、字符串與轉換為數字的函數int stoi(string)
int a=stoi("23");
1、對於char temp[10],可以用while(scanf("%s",temp)!=EOF)判斷讀取結束,輸入時要輸入Enter,Ctrl+Z,Enter標志結束。
----如:PAT乙級1007( 素數對猜想)、乙級1010(一元多項式求導)
2、字符數組和string相互轉換
#include<cstdio>
#include<string>
using namespace std;
int main(){
char str[]="hello";
//字符數組轉為string
string s(str);
printf("%s\n",str);
//string轉為字符數組用c_str()
printf("%s\n",s.c_str());
return 0;
}
4、平衡二叉樹調整、判斷一棵樹是不是完全二叉樹
1123 Is It a Complete AVL Tree
1066 Root of AVL Tree
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct Node{
int data,height;
Node* left,*right;
};
int getHeight(Node* root){
if(root==NULL){
return 0;
}
return root->height;
}
int getBalanceFactor(Node* root){
if(root==NULL) return 0;
return getHeight(root->left)-getHeight(root->right);
}
void updateHeight(Node* &root){
if(root!=NULL){
root->height=max(getHeight(root->left),getHeight(root->right))+1;
}
}
void L(Node* &root){
Node* temp=root->right;
root->right=temp->left;
temp->left=root;
updateHeight(root);
updateHeight(temp);
root=temp;
}
void R(Node* &root){
Node* temp=root->left;
root->left=temp->right;
temp->right=root;
updateHeight(root);
updateHeight(temp);
root=temp;
}
void insert(Node* &root,int a){
if(root==NULL){
root=new Node;
root->data=a;
root->height=1;
root->left=root->right=NULL;
return;
}
if(a<root->data){
insert(root->left,a);
updateHeight(root);
if(getBalanceFactor(root)==2){
if(getBalanceFactor(root->left)==1){
R(root);
}else{
L(root->left);
R(root);
}
}
}else{
insert(root->right,a);
updateHeight(root);
if(getBalanceFactor(root)==-2){
if(getBalanceFactor(root->right)==-1){
L(root);
}else{
R(root->right);
L(root);
}
}
}
}
bool flag=false;
void print(Node* root){
if(root==NULL) return;
if(flag==false){
flag=true;
}else{
printf(" ");
}
printf("%d",root->data);
}
bool isCBT(Node* root){
queue<Node*> q;
q.push(root);
bool flg=true;
while(!q.empty()){
Node* front=q.front();
q.pop();
print(front);
if(front!=NULL){
q.push(front->left);
q.push(front->right);
}else{
while(q.size()>0){
Node* tmp=q.front();
if(tmp!=NULL){
flg=false;
break;
}
q.pop();
}
}
}
return flg;
}
int main(){
int n;
scanf("%d",&n);
Node* root=NULL;
for(int i=0;i<n;i++){
int a;
scanf("%d",&a);
insert(root,a);
}
if(isCBT(root)){
printf("\nYES\n");
}else{
printf("\nNO\n");
}
return 0;
}
5、讀取輸入的小技巧
1022 Digital Library
6、分割一個字符串的笨方法
#include<string>
#include<iostream>
#include<cstdio>
using namespace std;
string ans[10];
int cnt=0;
void split(string key){
int j=0,p;
for(int i=0;i<key.length();i++){
if(key[i]==' '){
p=0;
char temp[10];
// cout<<j<<" "<<i<<" "<<cnt<<endl;
while(j<i){
temp[p++]=key[j++];
}
temp[p++]='\0';
string tmp(temp);
ans[cnt]=tmp;
j=i+1;
cnt++;
}
}
p=0;
while(j<key.length()){
ans[cnt][p++]=key[j++];
}
cnt++;
}
int main(){
string key="test code debug sort keywords";
split(key);
for(int i=0;i<cnt;i++){
cout<<ans[i]<<endl;
}
return 0;
}
4.LCA算法
定義:The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.
Given any two nodes in a binary tree, you are supposed to find their LCA.
1151 LCA in a Binary Tree (30 分)
5.根據前序遍歷和后序遍歷求中序遍歷
因為構造的二叉樹不唯一,可能有多個中序遍歷。
1119 Pre- and Post-order Traversals
#include <cstdio>
#include <vector>
using namespace std;
vector<int> ans;
int *pre, *post, unique = 1;//表示唯一
int findFromPre (int x, int l, int r) {
for (int i = l; i <= r; i++) {
if (x == pre[i]) {
return i;
}
}
return -1;
}
void setIn (int preL, int preR, int postL, int postR) {
if (preL == preR) {
ans.push_back(pre[preL]);
return;
}
if (pre[preL] == post[postR]) {//根節點
int x = findFromPre(post[postR - 1], preL + 1, preR);//從前序遍歷中找到右子樹的根節點位置x,則在前序遍歷中該節點之前都是左子樹,之后都是右子樹
int leftNum=x-preL-1;
if (leftNum>0) {//存在左子樹
setIn(preL + 1, x - 1, postL, postL + leftNum - 1);//遞歸地從左子樹進行
ans.push_back(post[postR]);//訪問根節點
setIn(x, preR, postL + leftNum, postR - 1);//遞歸地從右子樹進行
} else {
unique = 0;//不存在左子樹,則構造的右子樹不唯一
ans.push_back(post[postR]);//訪問根節點
setIn(x, preR, postL + leftNum, postR - 1);//遞歸地向右子樹進行
}
}
}
int main() {
int n = 0;
scanf("%d", &n);
pre = new int [n];
post = new int [n];
for (int i = 0; i < n; i++) {
scanf("%d", &pre[i]);
}
for (int i = 0; i < n; i++) {
scanf("%d", &post[i]);
}
setIn(0, n - 1, 0, n - 1);
printf("%s\n", unique ? "Yes" : "No");
printf("%d", ans[0]);
for (int i = 1; i < ans.size(); i++) {
printf(" %d", ans[i]);
}
printf("\n");
return 0;
}