当前位置:网站首页>November 17, 2021: the longest path of the same value. Given a binary tree, find the longest path
November 17, 2021: the longest path of the same value. Given a binary tree, find the longest path
2022-06-24 01:28:00 【Fuda scaffold constructor's daily question】
2021-11-17: The longest path of the same value . Given a binary tree , Find the longest path , Each node in this path has the same value . This path may or may not go through the root node . Be careful : The path length between two nodes is represented by the number of sides between them . Power button 687.
answer 2021-11-17:
recursive .
1.x irrelevant , The left maximum path and the right maximum path take the maximum value .
- x of . 2.1. x own . 2.2. Left (x)+x+ Right (x).
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
root := NewTreeNode(5)
root.left = NewTreeNode(4)
root.right = NewTreeNode(5)
root.left.left = NewTreeNode(1)
root.left.right = NewTreeNode(1)
root.right.right = NewTreeNode(5)
ret := longestUnivaluePath(root)
fmt.Println(ret)
}
type TreeNode struct {
val int
left *TreeNode
right *TreeNode
}
func NewTreeNode(v int) *TreeNode {
ret := &TreeNode{}
ret.val = v
return ret
}
func longestUnivaluePath(root *TreeNode) int {
if root == nil {
return 0
}
return process(root).max - 1
}
// Construction to x A tree headed by nodes , Return two messages
type Info struct {
// On one path : Each node is required to pass only once
len int // The path must be from x When you start and can only go down , The maximum distance of the path
max int // The path does not require that it must be from x In the case of departure , The maximum legal path distance of the whole tree
}
func NewInfo(l, m int) *Info {
ret := &Info{}
ret.len = l
ret.max = m
return ret
}
func process(x *TreeNode) *Info {
if x == nil {
return NewInfo(0, 0)
}
l := x.left
r := x.right
// On the left tree , It is not required to start from the left child , Maximum path
// On the left tree , We must start from the left child , The maximum path down
linfo := process(l)
// On the right tree , Don't ask to start from the right child , Maximum path
// On the right tree , We must start from the right child , The maximum path down
rinfo := process(r)
// Must be from x In the case of departure , The maximum path down
len0 := 1
if l != nil && l.val == x.val {
len0 = linfo.len + 1
}
if r != nil && r.val == x.val {
len0 = getMax(len0, rinfo.len+1)
}
// Not required from x set out , Maximum path
max := getMax(getMax(linfo.max, rinfo.max), len0)
if l != nil && r != nil && l.val == x.val && r.val == x.val {
max = getMax(max, linfo.len+rinfo.len+1)
}
return NewInfo(len0, max)
}
func getMax(a int, b int) int {
if a > b {
return a
} else {
return b
}
}The results are as follows :
边栏推荐
- ctfhub---SSRF
- Echo framework: implementing distributed log tracing
- Echo framework: automatically add requestid
- SAP executes PGI on the delivery order of STO and reports an error -fld selectn for Mvmt type 643 acct 400020 differences
- Feasibility of importing UE4 using gltf with instances
- Eight common errors in programming
- Talk about 11 tips for interface performance optimization
- Basic templates for various configurations of the SSM framework
- [planting grass by technology] 13 years' record of the prince of wool collecting on the cloud moving to Tencent cloud
- Theoretical analysis of countermeasure training: adaptive step size fast countermeasure training
猜你喜欢

ICML'22 | ProGCL: 重新思考图对比学习中的难样本挖掘

Isn't this another go bug?

JS input / output statements, variables

Installation and use of winscp and putty

LSF opens job idle information to view the CPU time/elapse time usage of the job

Error reported using worker: uncaught domexception: failed to construct 'worker': script at***

13 `bs_ duixiang. Tag tag ` get a tag object
Talk to Wu Jiesheng, head of Alibaba cloud storage: my 20 years of data storage (unlimited growth)
Shengdun technology joined dragon lizard community to build a new open source ecosystem
![2022 postgraduate entrance examination experience sharing [preliminary examination, school selection, re examination, adjustment, school recruitment and social recruitment]](/img/05/e204f526e2f3e90ed9a7ad0361a72e.png)
2022 postgraduate entrance examination experience sharing [preliminary examination, school selection, re examination, adjustment, school recruitment and social recruitment]
随机推荐
IIS installation and setup
Longest substring without duplicate characters
[technical grass planting] use webhook to automatically deploy my blogs on multiple sites in Tencent cloud
Note sharing (5) -precautions for Oracle to MySQL
Error reported using worker: uncaught domexception: failed to construct 'worker': script at***
Everything I see is the category of my precise positioning! Open source of a new method for saliency map visualization
4 most common automated test challenges and Countermeasures
ctfhub---SSRF
Batch generation of 2D codes from txt files
On November 11, 2021, live broadcast e-commerce opened a new way to play
What you don't know about traifik
对抗训练理论分析:自适应步长快速对抗训练
Echo framework: implementing timeout Middleware
【Flutter】如何使用Flutter包和插件
Architecture solutions
js输入输出语句,变量
How to self-study website construction is website construction company reliable
How do small businesses do a good job in website construction? Is there any guarantee for network companies to build websites
November 20, 2021: the start and end times of a movie can be listed in a small array
Remove the cloud disk service display "continued" logo