当前位置:网站首页>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 :
边栏推荐
- Leetcode lecture on algorithm interview for large factories 2 Time space complexity
- How to open Tencent enterprise mailbox and Tencent enterprise mailbox login portal
- [technical grass planting] use webhook to automatically deploy my blogs on multiple sites in Tencent cloud
- One article introduces you to the world of kubernetes
- Pad User Guide
- How to implement NSQ delay streaming technology in easycvr?
- Openstack
- How to realize IP invariance in the private network of basic network ECs and cloud database resource switching
- Spatial4j introduction practice
- 跨域和JSONP
猜你喜欢

Icml'22 | progcl: rethinking difficult sample mining in graph contrast learning
![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]

js输入输出语句,变量

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

Perhaps the greatest romance of programmers is to commemorate their dead mother with a software

用一个软件纪念自己故去的母亲,这或许才是程序员最大的浪漫吧

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

Isn't this another go bug?

Arm learning (7) symbol table and debugging

Pad User Guide
随机推荐
How does smart digital operation get through offline collection and online marketing?
13 `bs_duixiang.tag标签`得到一个tag对象
Solution of self Networking Wireless Communication intercom system in Beifeng oil and gas field
Attack and defense world PyC trade
How do small businesses do a good job in website construction? Is there any guarantee for network companies to build websites
CDN access log quality performance monitoring and operation statistical analysis best practices
An accident caused by a MySQL misoperation, and the "high availability" cannot withstand it!
2021-11-19:[0,4,7]:0 means that the stone here has no color. If it turns red
Remember the performance optimization with 18 times improvement at one time
Golang gets the start timestamp and end timestamp of a past or future week or month
Note sharing (5) -precautions for Oracle to MySQL
jdbc
[planting grass by technology] 13 years' record of the prince of wool collecting on the cloud moving to Tencent cloud
CSDN articles crawl the top ten bloggers' articles and convert them to MD
Dart series: metaworld pubspec Yaml file details
Basic templates for various configurations of the SSM framework
How to quickly convert stock code into int in quantitative trading?
IIS installation and setup
Mobile direct payment, super convenient
Location and troubleshooting of memory leakage: analysis of heap profiling principle