当前位置:网站首页>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
边栏推荐
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- Why do job hopping take more than promotion?
- [interpretation of the paper] machine learning technology for Cataract Classification / classification
- b站视频链接快速获取
- 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
- Forward maximum matching method
- Is it profitable to host an Olympic Games?
- Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
- Opencv learning example code 3.2.3 image binarization
- El table table - get the row and column you click & the sort of El table and sort change, El table column and sort method & clear sort clearsort
猜你喜欢
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
爱可可AI前沿推介(7.6)
None of the strongest kings in the monitoring industry!
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
缓存更新策略概览(Caching Strategies Overview)
Seven original sins of embedded development
嵌入式开发的7大原罪
Interviewer: what is the internal implementation of ordered collection in redis?
Infrared thermometer based on STM32 single chip microcomputer (with face detection)
966 minimum path sum
随机推荐
for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
document. Usage of write () - write text - modify style and position control
字符串的使用方法之startwith()-以XX开头、endsWith()-以XX结尾、trim()-删除两端空格
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
代理和反向代理
互联网快讯:吉利正式收购魅族;胰岛素集采在31省全面落地
This year, Jianzhi Tencent
Pat 1078 hashing (25 points) ⼆ times ⽅ exploration method
如何实现常见框架
Nodejs教程之让我们用 typescript 创建你的第一个 expressjs 应用程序
Forward maximum matching method
Replace Internet TV set-top box application through digital TV and broadband network
OAI 5g nr+usrp b210 installation and construction
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
c#使用oracle存储过程获取结果集实例
在最长的距离二叉树结点
el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
Four common ways and performance comparison of ArrayList de duplication (jmh performance analysis)