当前位置:网站首页>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
边栏推荐
- 嵌入式开发的7大原罪
- [interpretation of the paper] machine learning technology for Cataract Classification / classification
- VIM basic configuration and frequently used commands
- ACdreamoj1110(多重背包)
- Nodejs教程之Expressjs一篇文章快速入门
- PG basics -- Logical Structure Management (transaction)
- 【mysql】触发器
- [MySQL] trigger
- Set up a time server
- 防火墙基础之外网服务器区部署和双机热备
猜你喜欢
2017 8th Blue Bridge Cup group a provincial tournament
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
KDD 2022 | realize unified conversational recommendation through knowledge enhanced prompt learning
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
Common English vocabulary that every programmer must master (recommended Collection)
3D人脸重建:从基础知识到识别/重建方法!
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Set up a time server
随机推荐
Deployment of external server area and dual machine hot standby of firewall Foundation
Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,
R语言做文本挖掘 Part4文本分类
for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
JS operation DOM element (I) -- six ways to obtain DOM nodes
在最长的距离二叉树结点
Why do job hopping take more than promotion?
Math symbols in lists
@Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
Mtcnn face detection
MySQL - 事务(Transaction)详解
Three schemes of SVM to realize multi classification
This year, Jianzhi Tencent
缓存更新策略概览(Caching Strategies Overview)
967- letter combination of telephone number
Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
039. (2.8) thoughts in the ward
Pat 1085 perfect sequence (25 points) perfect sequence
Forward maximum matching method