当前位置:网站首页>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 .
边栏推荐
- Nailing open platform - applet development practice (nailing applet server side)
- 08_SpingBoot 集成Redis
- [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
- Database design (I)
- Mobile terminal pull-down loading pull-down loading data
- BSC 及HT 等链的NFT 创造及绑定图片教程
- Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview
- Yapi cross domain request plug-in installation
- Install SVN in Pagoda and build SVN version Library
- Add, delete, modify and query curd in PHP native SQL
猜你喜欢

2021/11/6-burpsuit packet capturing and web page source code modification

NPM installation tutorial
![[Qunhui] no port access (reverse proxy + intranet penetration)](/img/bc/b1e0c5c382e30fbcea28fbc68c1151.jpg)
[Qunhui] no port access (reverse proxy + intranet penetration)

Physical design of database design (2)

Swagger

Performance test comparison between PHP framework jsnpp and thinkphp6

六、项目实战---识别猫和狗

mysql高级学习(跟着尚硅谷老师周阳学习)
![[geek challenge 2019] rce me](/img/92/978c54fb42391198300c76ae92893d.jpg)
[geek challenge 2019] rce me

一幅脑图总结一下需求分析(工作上实际遇到的情况的补充)
随机推荐
Create alicloud test instances
小程序中实现视频通话及互动直播功能
Laravel access error could not be opened
2021-01-31
SQL related knowledge - DDL
SQL related knowledge - constraints
A brain map to summarize the needs analysis (a supplement to the actual situation at work)
I like you!
mysql高级学习(跟着尚硅谷老师周阳学习)
Mutex of thread synchronization (mutex)
1064 (42000) error occurred when installing MySQL and modifying root password
SQL related knowledge - DQL
What should I do if I don't understand the precious metal indicators
How to carry out word-of-mouth marketing for enterprises' products and services? Can word of mouth marketing be done on behalf of others?
6、 Project practice --- identifying cats and dogs
Database design (3): database maintenance and optimization
Advanced learning of MySQL (learning from Shang Silicon Valley teacher Zhou Yang)
The open software of win10 system is too small. How to make it larger (effective through personal test)
Knowledge about SQL - DML
Introduction to markdown grammar