当前位置:网站首页>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 .
边栏推荐
- There is no response to redirection and jump in the laravel constructor [original]
- A brain map to summarize the needs analysis (a supplement to the actual situation at work)
- 排序查询
- Review of number theory
- The statistics in the MySQL field become strings, and then they are converted into numbers for sorting
- [learn FPGA programming from scratch -45]: vision chapter - integrated circuits help high-quality development in the digital era -2- market forecast
- Report on demand situation and development trend of China's OTC industry from 2022 to 2028
- numpy 通用函数
- Database design (3): database maintenance and optimization
- [H5 development] 02 take you to develop H5 list page ~ including query, reset and submission functions
猜你喜欢
![Fastadmin always prompts sqlstate[23000]: integrity constraint violation: 1052 column 'ID' in order clause is am](/img/71/ed3b2ca9e6d4afd2dae3c601b3a75b.jpg)
Fastadmin always prompts sqlstate[23000]: integrity constraint violation: 1052 column 'ID' in order clause is am

6、 Project practice --- identifying cats and dogs

08_SpingBoot 集成Redis

一幅脑图总结一下需求分析(工作上实际遇到的情况的补充)
![[Qunhui] no port access (reverse proxy + intranet penetration)](/img/bc/b1e0c5c382e30fbcea28fbc68c1151.jpg)
[Qunhui] no port access (reverse proxy + intranet penetration)

Yapi cross domain request plug-in installation

A brain map to summarize the needs analysis (a supplement to the actual situation at work)

PIP batch complete uninstall package

Understand CGI and fastcgi

NPM installation tutorial
随机推荐
Composer version rollback version switching
Minecraft 1.16.5 生化8 模组 1.9版本 1.18版本同步
PHP design function getmaxstr to find the longest symmetric string in a string - [original]
Knowledge of SQL - database design, backup and restore
mysql高级学习(跟着尚硅谷老师周阳学习)
Swagger
How to carry out word-of-mouth marketing for enterprises' products and services? Can word of mouth marketing be done on behalf of others?
Oracle 數據泵導錶
Ueeditor automatically appends P tags to rich text.br tags always wrap.br tag solutions
Analysis report on the development trend and operation status of China's environmental monitoring instrument industry from 2022 to 2028
CTF crypto (I) some simple encoding and encryption
Ubuntu installs PostgreSQL and uses omnidb to view
NFT creation and binding of BSC and HT chains
小程序中实现视频通话及互动直播功能
numpy 数据输入输出
2022 talent strategic transformation under the development trend of digital economy
Laravel access error could not be opened
Laravel file stream download file
Zeromq from getting started to mastering
Nabicat连接:本地Mysql&&云服务Mysql以及报错