当前位置:网站首页>1.20 learning summary
1.20 learning summary
2022-06-26 04:35:00 【After all, I still walk alone】
Today I'm going to solve the problem again . What I want to say today is a Through the program to determine the depth of the binary tree . The specific topics are as follows .
The way I did this problem . Is to build a binary tree directly from a structure . To find the depth by preorder traversal .
One was used max function . And used a deep Variable to record the current depth . max Function is to get the maximum value of each time In this way, you can find the depth after traversing the entire function .
The specific code is as follows ,
#include<stdio.h>
#include <algorithm>
using namespace std;
struct node {
int left, right;
};
node tree[10000000];
int n, ans;
void dfs(int d, int deep) {
if (d == 0) return ;
ans = max(ans, deep);
dfs(tree[d].left, deep+1);
dfs(tree[d].right, deep+1);// Use preorder to traverse
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&tree[i].left,&tree[i].right);// A forest is represented by a structure .
}
dfs(1, 1);
printf("%d",ans);
return 0;
}
Of course, my code has a little logic problem . This code cannot find the root node . That is to say, the first input must be the root node . I solved this problem while working on another topic . But when I read this topic , Didn't study it carefully . When I was doing another topic , That solves the problem . Specifically speaking ? Is to create an additional data field to store . Their root node It is equivalent to each subtree . Stored their last node . That is, relative to their root node . In this case , There are only root nodes in the whole tree , There is no ancestor . You can find the root node in the structure .
Now that it's all here , Just solve the problem . Did it, too . The title is as follows .
And the solution to this problem I also said a little . The only thing to add is that the elements here are mostly characters , Or all letters . Then I use the method of forced transformation . Turn them into int type Thus, the subscript of the lower structure can be used ,
Then the way to store the number of pieces . It's what I call building three data fields , Store his ancestors separately . And left and right nodes . And then traverse it . Then the general idea is like this . The code is as follows ,
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct node{
char left,right,ss;
};
node tree[1000];
void dfs(char s)
{
printf("%c", s);
if (tree[s].left!='*') dfs(tree[s].left);
if (tree[s].right!='*') dfs(tree[s].right);
}
int main()
{
int i,n;
char c,f1;
char d[10000];
cin>>n;
for (i=1;i<=n;i++){
cin>>c;
d[i]=c;
cin>>tree[c].left>>tree[c].right;
tree[tree[c].left].ss = tree[tree[c].right].ss = c;
}for(i=1;i<=n;i++){
if(tree[d[i]].ss==0){
f1=d[i];
}
}
dfs (f1);
return 0;
}
That's all for today's summary . I will also work out all the solutions for these days . I will do my best , The work was done in detail . It is also the second time to understand these topics .
边栏推荐
- Zhubo Huangyu: all the precious metals you want to know are here
- BSC 及HT 等链的NFT 创造及绑定图片教程
- mysql高级学习(跟着尚硅谷老师周阳学习)
- Navicat connects the pit of shardingsphere sub table and sub library plug-ins
- Resolve PHP is not an internal or external command
- Etcd watch principle
- 2021/11/6-burpsuit packet capturing and web page source code modification
- Tp6 controller does not exist: app\index\controller\index
- Knowledge of SQL - database design, backup and restore
- How to carry out word-of-mouth marketing for enterprises' products and services? Can word of mouth marketing be done on behalf of others?
猜你喜欢
Tp6 controller does not exist: app\index\controller\index
Fastadmin always prompts sqlstate[23000]: integrity constraint violation: 1052 column 'ID' in order clause is am
Redis cache data consistency solution analysis
Realize video call and interactive live broadcast in the applet
Install dbeaver and connect Clickhouse
2022 talent strategic transformation under the development trend of digital economy
ctf [RoarCTF 2019]easy_ calc
2020-12-18
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
OSS CDN alicloud configuration method
随机推荐
Knowledge of functions
Guide de la pompe de données Oracle
Svn correlation
Svn error command revert error previous operation has not finished; run ‘ cleanup‘ if
Laravel uses phpword to generate word documents
Mysql8.0 configuring my SQL in INI file_ mode=NO_ AUTO_ CREATE_ User can start
[H5 development] 01 take you to experience H5 development from a simple page ~ the whole page implementation process from static page to interface adjustment manual teaching
Alipay failed to verify the signature (sandbox test indicates fishing risk?) [original]
Thinkphp6 using kindeditor
The open software of win10 system is too small. How to make it larger (effective through personal test)
Upload script file (one sentence back door) WAF bypass (PHP)
[Qunhui] no port access (reverse proxy + intranet penetration)
Zhubo Huangyu: you can try these non-agricultural operation skills
Database design (I)
CTF serialization and deserialization
List of provinces, cities and counties in China
SQL related knowledge - constraints
Install cenos in the virtual machine
Nabicat连接:本地Mysql&&云服务Mysql以及报错
Create alicloud test instances