当前位置:网站首页>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 :
边栏推荐
- Echo framework: implementing timeout Middleware
- ctfhub---SSRF
- One article introduces you to the world of kubernetes
- LSF opens job idle information to view the CPU time/elapse time usage of the job
- Talk to Wu Jiesheng, head of Alibaba cloud storage: my 20 years of data storage (unlimited growth)
- How to view kubernetes API traffic by grabbing packets
- [solution] how to realize AI automatic recognition of high altitude parabolic behavior?
- 什么是养老理财?养老理财产品有哪些?
- Esp8266 OTA remote and wireless upgrade
- How to build a practical website and how to operate after the website goes online
猜你喜欢

Cross domain and jsonp

对抗训练理论分析:自适应步长快速对抗训练

【Flutter】如何使用Flutter包和插件

Icml'22 | progcl: rethinking difficult sample mining in graph contrast learning

Open source model library of flying propeller industry: accelerating the development and application of enterprise AI tasks

Everything I see is the category of my precise positioning! Open source of a new method for saliency map visualization

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

Isn't this another go bug?

Arm learning (7) symbol table and debugging
Talk to Wu Jiesheng, head of Alibaba cloud storage: my 20 years of data storage (unlimited growth)
随机推荐
Server performance monitoring: Best Practices for server monitoring
Error reported using worker: uncaught domexception: failed to construct 'worker': script at***
2021-11-21: map[i][j] = = 0, which means that (I, J) is an ocean. If you cross it, the cost will be
Map design
Build a smart drug supervision platform based on easycvr video technology and build a drug safety firewall
How to build a "preemptive" remote control system (- - memory chapter)
2022 postgraduate entrance examination experience sharing [preliminary examination, school selection, re examination, adjustment, school recruitment and social recruitment]
PVE enables the hardware graphics card pass through function
js输入输出语句,变量
How to realize IP invariance in the private network of basic network ECs and cloud database resource switching
Textplus - reverse engineering of textplus
Part of the problem solution of unctf2020
What is pension finance? What are the pension financial products?
CSDN articles crawl the top ten bloggers' articles and convert them to MD
CODING CD
[flutter] comment utiliser les paquets et plug - ins flutter
How do small businesses do a good job in website construction? Is there any guarantee for network companies to build websites
Basic templates for various configurations of the SSM framework
Note sharing (5) -precautions for Oracle to MySQL
What is the website construction process? What details need to be valued?