当前位置:网站首页>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 .
边栏推荐
- Create alicloud test instances
- 2021/11/6-burpsuit packet capturing and web page source code modification
- Add, delete, modify and query curd in PHP native SQL
- 2021-02-07
- Fastadmin always prompts sqlstate[23000]: integrity constraint violation: 1052 column 'ID' in order clause is am
- 35岁程序员炒Luna 千万资产3天归零,网友:和赌博一样
- PHP has the problem of using strtotime to obtain time in months and months [original]
- Laravel uses phpword to generate word documents
- SQL related knowledge - DDL
- There is no response to redirection and jump in the laravel constructor [original]
猜你喜欢

Realize video call and interactive live broadcast in the applet

Physical design of database design (2)

2021-02-07

MySQL enable logbin in Qunhui docker
![[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](/img/e4/27611abdd000019e70f4447265808c.jpg)
[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

Construction of art NFT trading platform | NFT mall
![[H5 development] 02 take you to develop H5 list page ~ including query, reset and submission functions](/img/39/64df931d5ec54d7d19ae444fa372ba.jpg)
[H5 development] 02 take you to develop H5 list page ~ including query, reset and submission functions

Microsoft prohibits Russian users from downloading and installing win10/11

SixTool-多功能多合一代挂助手源码

NPM installation tutorial
随机推荐
Development prospect and investment strategic planning report of global and Chinese PVC hose industry from 2022 to 2028
SQL related knowledge - DDL
Thymeleaf data echo, single selection backfill, drop-down backfill, time frame backfill
Install dbeaver and connect Clickhouse
numpy 索引及切片
CTF serialization and deserialization
Mutex of thread synchronization (mutex)
"Eight hundred"
pip 批量完全卸载包
Zhimeng CMS will file a lawsuit against infringing websites
Implementation of seven classes of BlockingQueue interface
numpy 通用函数
修改Oracle连接数
A troubleshooting of website crash due to high CPU
Performance test comparison between PHP framework jsnpp and thinkphp6
Review of number theory
A brain map to summarize the needs analysis (a supplement to the actual situation at work)
基础查询
Ueeditor automatically appends P tags to rich text.br tags always wrap.br tag solutions
The statistics in the MySQL field become strings, and then they are converted into numbers for sorting