目录
左叶子的和
我真的会爱惨懒猫老师
序 : 读入的处理+创建二叉树
不用构建类来写二叉树啦!
这学期学类就经常会被要求使用类来封装。至于考试的时候,其实想写也可以,不写代码更加简洁。
之前有文章写链表+类构建二叉树
今天就单独为做题服务
//结构体
struct Node{
int data;
Node *lchild,*rchild;
}
//之前创建节点的方式递归一下就可以创建二叉树
Node* create(Node* bt){
char ch=getchar();
if(ch==',') ch=getchar();
if(ch=='#) return NULL;
else{
int a;
while(ch!=','){
a=a*10+ch-'0';
ch=getchar();
}
}
return bt;
}
int main(){
Node* root;
root=create(root);
}
key:
1.这里的读入是靠“,”分割且涉及到字符与数字的转化。
2.看清这里两个if的功效
if(ch==',') ch=getchar();
if(ch=='#') return NULL;
else{
...
}
另一种大同小异写法,不过是用了scanf():
char key;
int keyi=0;
scanf("%c",&key);
while(key==',') scanf("%c",&key);
while('0'<=key&&key<='9'){
keyi=keyi*10+key-'0';
scanf("%c",&key);
}
if(key=='#') return NULL;
else{
//创建该节点
//左孩子
//右孩子
}
思路
不管怎么样我们都要先明确左叶子的含义(像本人考试的时候就没有看清楚):
没有度(没有孩子),并且是在父节点的左边。
判定条件:
lchild==NULL&&rchild==NULL,再加上“在父节点的左边”。
基于红字我们衍生出来两种方法
解法一:标记法
因为不能回溯到父节点,这就需要我们在从父节点传参的时候,使用flag标记左右。
int getLeftNum(Node* bt,bool flag){
if(bt==NULL){
return 0;
}
int sum=0;
if(bt->lchild==NULL&&bt->rchild==NULL&&flag){
sum+=bt->data;
}
sum+=getLeftNum(bt->lchild,true);
sum+=getLeftNum(bt->rchild,false);
return sum;
}
(我的参考代码用的static,我不是很会这个,就换了一种写法)
解法二:预判法
简言之就是,在我这个节点这里,直接判断我的孙子。
int getLeftNum(Node* bt){
if(bt==NULL) return 0;
int sum=0;
if(bt->child!=NULL&&bt->lchild->lchild==NULL&&bt->lchild->rchild==NULL)
sum+=bt->lchild;
sum+=getLeftNum(bt->lchild);
sum+=getLeftNum(bt->rchild);
return sum;
}
综上我们可以得到一个可A的代码
#include<iostream>
using namespace std;
struct Node{
int data;
Node *lchild,*rchild;
};
Node* creat(Node* bt){
char ch=getchar();
if(ch==',') ch=getchar();
if(ch=='#') return NULL;
else{
int a=0;
while(ch!=','){
a=a*10+ch-'0';
ch=getchar();
}
bt=new Node;
bt->data=a;
bt->lchild=creat(bt->lchild);
bt->rchild=creat(bt->rchild);
}
return bt;
}
//int getLeftNum(Node* bt){
// if(bt==NULL) return 0;
// int sum=0;
// if(bt->lchild!=NULL&&bt->lchild->lchild==NULL&&bt->lchild->rchild==NULL)
// sum+=bt->lchild->data;
// sum+=getLeftNum(bt->lchild)+getLeftNum(bt->rchild);
// return sum;
//}
int getLeftNum(Node* bt,bool flag){
if(bt==NULL){
return 0;
}
int sum=0;
if(bt->lchild==NULL&&bt->rchild==NULL&&flag){
sum+=bt->data;
}
sum+=getLeftNum(bt->lchild,true);
sum+=getLeftNum(bt->rchild,false);
return sum;
}
int main(){
Node* root=NULL;
root=creat(root);
cout<<getLeftNum(root,true);
return 0;
}
解法三:另辟蹊径
本来想画个图的,好不容易画的图被吃了。
简单汪两句大陈曦老师的解法:基于转化之后一维数组实现左叶节点求和。
得到一个一维数组后,我们可以很容易发现,左节点都是偶数耶。(依旧是下标从1开始)
于是我们只需要验证偶数下标是否有孩子。
注意!这里发现一个比较巧妙的点。当你把空都赋值为0时,就不用判断惹。
int getArray(Node* bt,int *array,int n){
if(bt==NULL) return n/2;
array[n]=bt->data;
int left=getArray(bt->lchild,array,n*2);
int right=getArray(bt->rchild,array,n*2+1);
return max(left,right);
}
int getLeftNum1(int *array,int len){
int sum=0;
for(int i=1;i<=len;i++){
if(i%2==0){
sum+=array[i];
}
}
return sum;
}
得到一维数组的函数可以看我之前的文章。
完全二叉树最近的公共祖先
嗷嗷嗷嗷这题,真的lip,我改了好久,思路出现了偏差
省流:下标从1开始,如果不相等就让大的那个/2(取他的父节点)。然后输入其中一个其中一个(两个下标最后就相等了)
呜呜呜呜我之前的思路就是,如果不相等就让两个一起/2,没有考虑到其中一个是另一个祖先的这种情况!(甚至题干中还有提示)
#include<iostream>
#include<string.h>
using namespace std;
const int N=1e5+10
int main(){
int n;
cin>>n;
char s[N];
for(int i=1;i<=n;i++) cin>>s[i];
char x,y;
int a,b;
cin>>x>>y;
//找到下标
for(int i=1i<=n;i++){
if(s[i]==x) a=i;
if(s[i]==y) b=i;
}
while(a!=b){
if(a>b) a/=2;
else b/=2;
}
cout<<s[a];
return 0;
}
栈的应用-模拟工厂
这里就是模拟栈的压栈弹栈。
#include<iostream>
using namespace std;
const int N=1e5+10;
int main(){
int a[N];
int top=-1;
int n,m,bianhao=1,caozuo;
cin>>n>>m;
while(m--){
cin>>caozuo;
if(caozuo==1&&bianhao<=n){
a[++top]=bianhao++;
}
else if(caozuo==2&&top!=-1){
cout<<a[top--]<<" ";
}
}
return 0;
}
注意:
题干已经给出特判条件,
1.编号为1~n---->不能超过编号n
2.如果没有就不操作---->栈为空时,不操作
哈夫曼树的带权路径长度(WPL)
什么是带权路径长度嘞
1、路径和路径长度
在一棵树中,从一个结点往下可以达到的孩子或孙子结点之间的通路,称为路径。通路中分支的数目称为路径长度。若规定根结点的层数为 11,则从根结点到第 LL 层结点的路径长度为 L−1L−1。
2、结点的权及带权路径长度
若将树中结点赋给一个有着某种含义的数值,则这个数值称为该结点的权。结点的带权路径长度为:从根结点到该结点之间的路径长度与该结点的权的乘积。
3、树的带权路径长度
树的带权路径长度规定为所有叶子结点的带权路径长度之和,记为 WPL。
使用哈夫曼构造方法
使用优先对列
先补一补STL里面优先队列的用法
可以瞅瞅上面的博文hhhh(会用就行)
这里的方法推理大概就是
简单汪两句:
可以看到3+5+10+19=37等价于9*1+5*2+2*3+1*4+2*4=37.而后者是根据构造哈夫曼树之后写出来的计算路径的式子。
仔细观察,19是不是子树中所有叶子节点的和!!同样10、5(非叶子节点)、3是不是都有这种情况!
不行,还是得解释清楚一点:
得到19的过程:1、2、2、5、9都加过一次对吧。
得到10的过程:1、2、2、5都加过一次对吧。
5:1、2、2加过一次
3:1、2加过一次
把上述左边加左边右边加右边:
因为他们的这种特性,我们可以很容易得出,带权路径长度就等于所有非叶子节点之和。(看上面的图)
于是,使用优先队列,每次找到最小x和次小y的数,把他们相加(x+y)放入队列,然后sum+=x+y;
#include<iostream>
using namespace std;
int main(){
priority_queue<int,vector<int>,greater<int> > q;
int n,val,sum=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>val;
q.push(val);
}
while(q.size()>1){
int x=q.top(); q.pop();
int y=q.top(); q.pop();
q.push(x+y);
sum+=x+y;
}
cout<<sum<<endl;
return 0;
}