当前位置:网站首页>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
边栏推荐
- In JS, string and array are converted to each other (II) -- the method of converting array into string
- 3D人脸重建:从基础知识到识别/重建方法!
- Regular expression collection
- [sliding window] group B of the 9th Landbridge cup provincial tournament: log statistics
- 3D人脸重建:从基础知识到识别/重建方法!
- 通过数字电视通过宽带网络取代互联网电视机顶盒应用
- el-table表格——sortable排序 & 出现小数、%时排序错乱
- Replace Internet TV set-top box application through digital TV and broadband network
- @Detailed differences among getmapping, @postmapping and @requestmapping, with actual combat code (all)
- Aiko ai Frontier promotion (7.6)
猜你喜欢

3D人脸重建:从基础知识到识别/重建方法!

Aike AI frontier promotion (7.6)
![[interpretation of the paper] machine learning technology for Cataract Classification / classification](/img/0c/b76e59f092c1b534736132faa76de5.png)
[interpretation of the paper] machine learning technology for Cataract Classification / classification

【mysql】触发器

966 minimum path sum

SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍

面试官:Redis中有序集合的内部实现方式是什么?

LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件

SAP UI5 框架的 manifest.json

Absolute primes (C language)
随机推荐
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
Performance test process and plan
OneNote 深度评测:使用资源、插件、模版
Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
@GetMapping、@PostMapping 和 @RequestMapping详细区别附实战代码(全)
Swagger UI tutorial API document artifact
VIM basic configuration and frequently used commands
SDL2来源分析7:演出(SDL_RenderPresent())
3D人脸重建:从基础知识到识别/重建方法!
OSPF multi zone configuration
'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
FZU 1686 龙之谜 重复覆盖
Aiko ai Frontier promotion (7.6)
【力扣刷题】32. 最长有效括号
[go][转载]vscode配置完go跑个helloworld例子
Seven original sins of embedded development
Reinforcement learning - learning notes 5 | alphago
966 minimum path sum