当前位置:网站首页>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
边栏推荐
- 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
- How do I remove duplicates from the list- How to remove duplicates from a list?
- New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
- 【力扣刷题】32. 最长有效括号
- 跨分片方案 总结
- 14年本科毕业,转行软件测试,薪资13.5K
- Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
- 3D人脸重建:从基础知识到识别/重建方法!
- Proxy and reverse proxy
- ICML 2022 | flowformer: task generic linear complexity transformer
猜你喜欢
Common English vocabulary that every programmer must master (recommended Collection)
[in depth learning] pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
967- letter combination of telephone number
基于深度学习的参考帧生成
爱可可AI前沿推介(7.6)
Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?
【力扣刷题】一维动态规划记录(53零钱兑换、300最长递增子序列、53最大子数组和)
OneNote 深度评测:使用资源、插件、模版
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
随机推荐
JS operation DOM element (I) -- six ways to obtain DOM nodes
[MySQL] basic use of cursor
【mysql】游标的基本使用
Why do job hopping take more than promotion?
Aiko ai Frontier promotion (7.6)
在最长的距离二叉树结点
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
互联网快讯:吉利正式收购魅族;胰岛素集采在31省全面落地
Word bag model and TF-IDF
JS traversal array and string
Nodejs tutorial expressjs article quick start
ICML 2022 | flowformer: task generic linear complexity transformer
Replace Internet TV set-top box application through digital TV and broadband network
OSPF多区域配置
3D人脸重建:从基础知识到识别/重建方法!
通过数字电视通过宽带网络取代互联网电视机顶盒应用
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
【Redis设计与实现】第一部分 :Redis数据结构和对象 总结
Is this the feeling of being spoiled by bytes?
What are RDB and AOF