当前位置:网站首页>Binary tree node at the longest distance
Binary tree node at the longest distance
2022-07-06 21:18:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
It's divided into two parts :① The longest distance after root
② There is no longest distance root,
1. If the path passes through the root Root. be U and V It belongs to different subtrees , And they are the farthest nodes from the root node of the subtree . Otherwise, it is contradictory to their farthest distance . Such a situation is shown in the figure 3-13 Seen :
2. Assume that the path does not pass Root. Then they must belong to the root K One of the subtrees .
And they are also the two farthest vertices in the subtree . Pictured 3-14 The nodes in the A:
Set the first K The two farthest nodes in the tree :Uk and Vk, Its distance is defined as d(Uk,Vk), Then the node Uk or Vk That is, subtree K To the root node Rk The node with the longest distance . No loss of generality . We set up Uk For the subtree K Middle path root node Rk The node with the longest distance . The distance to the root node is defined as d(Uk,R). take d(Ui,R)(1<=i<=k) The two largest values in max1 and max2. Then go through the root node R The longest path of is max1+max2+2, So the tree R The distance between the two farthest points in is :max{d(U1,V1),…, d(Uk,Vk),max1+max2+2}.
Depth first search is adopted, as shown in the figure 3-15, You only need to traverse all nodes once , The time complexity is O(|E|)=O(|V|-1), among V Is the set of points .E A collection of sides .
Copyright notice : This article is the original article of the blogger . Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117115.html Link to the original text :https://javaforall.cn
边栏推荐
猜你喜欢
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
Aike AI frontier promotion (7.6)
OAI 5g nr+usrp b210 installation and construction
跨分片方案 总结
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
20220211 failure - maximum amount of data supported by mongodb
面试官:Redis中有序集合的内部实现方式是什么?
3D人脸重建:从基础知识到识别/重建方法!
【mysql】游标的基本使用
966 minimum path sum
随机推荐
Le langage r visualise les relations entre plus de deux variables de classification (catégories), crée des plots Mosaiques en utilisant la fonction Mosaic dans le paquet VCD, et visualise les relation
互联网快讯:吉利正式收购魅族;胰岛素集采在31省全面落地
None of the strongest kings in the monitoring industry!
JS学习笔记-OO创建怀疑的对象
Absolute primes (C language)
[go][转载]vscode配置完go跑个helloworld例子
2017 8th Blue Bridge Cup group a provincial tournament
Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
js 根据汉字首字母排序(省份排序) 或 根据英文首字母排序——za排序 & az排序
Reference frame generation based on deep learning
OAI 5g nr+usrp b210 installation and construction
[200 opencv routines] 220 Mosaic the image
Proxy and reverse proxy
Vim 基本配置和经常使用的命令
Mtcnn face detection
基于深度学习的参考帧生成
Nodejs tutorial let's create your first expressjs application with typescript
Pat 1085 perfect sequence (25 points) perfect sequence
14年本科毕业,转行软件测试,薪资13.5K
Interviewer: what is the internal implementation of ordered collection in redis?