当前位置:网站首页>【二叉树】二叉树中的最长交错路径
【二叉树】二叉树中的最长交错路径
2022-07-26 04:02:00 【豪冷啊】
0x00 题目
给你一棵以 root 为根的二叉树,二叉树中的交错路径定义如下:
选择二叉树中 任意 节点和一个方向(左或者右)
如果前进方向为右,那么移动到当前节点的的右子节点
否则移动到它的左子节点
改变前进方向:左变右或者右变左
重复第二步和第三步,直到你在树中无法继续移动
交错路径的长度定义为:
访问过的节点数目 - 1(单个节点的路径长度为 0 )
请你返回给定树中最长 交错路径 的长度
0x01 思路
采用深度优先遍历的方式
遍历下一个子节点时
是能区分出该节点是左节点,还是右节点的
通过为每个节点缓存 2 个值
一个左值 left,表示本节点为左节点时,到达所需路径数
一个右值 right,表示本节点为右节点时,到达所需路径数
因为节点要么是左节点,要么是右节点
所以两个值,必需有一个为 0
另外一个,根据本节点是左是右来确定
是左节点,则使用父节点的右值 + 1 (rihgt + 1)
是右节点,则使用父节点的左值 + 1 (left + 1)
最后使用一个全局变量
保存最大值即可
0x02 解法
语言:Swift
树节点:TreeNode
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
self.val = val
self.left = left
self.right = right
}
}
解法:
func longestZigZag(_ root: TreeNode?) -> Int {
var res: Int = 0
func dfs(_ root: TreeNode?, _ left: Int, _ right: Int) {
// 最大长度
res = max(res, max(left, right))
// 左子树存在,往右
if let l = root?.left {
dfs(l, right+1, 0)
}
// 右子树存在,往左
if let r = root?.right {
dfs(r, 0, left+1)
}
}
dfs(root, 0, 0)
return res
}
0x03 我的小作品
欢迎体验我的作品之一:小五笔
五笔学习好帮手!App Store 搜索即可~
边栏推荐
- 1311_ Hardware design_ Summary of ICT concept, application, advantages and disadvantages
- 【Unity3d Shader】角色投影与倒影
- [programmers must] Tanabata confession strategy: "the moon meets the cloud, the flowers meet the wind, and the night sky is beautiful at night". (with source code Collection)
- [digital ic/fpga] Hot unique code detection
- zkEVM:MINA的CEO对zkEVM和L1相关内容的总结
- What are the differences between vite and wenpack?
- (翻译)按钮位置约定能强化用户使用习惯
- Leetcode: 102. Sequence traversal of binary tree
- The convolution kernel is expanded to 51x51, and the new CNN architecture slak counterattacks the transformer
- [深入研究4G/5G/6G专题-42]: URLLC-13-《3GPP URLLC相关协议、规范、技术原理深度解读》-7-低延时技术-1-子载波间隔扩展
猜你喜欢
![[Reading Notes - > data analysis] 01 introduction to data analysis](/img/50/622878bf649e77d5a4fa9732fa6f92.png)
[Reading Notes - > data analysis] 01 introduction to data analysis

Booking.com binke Shanghai noodles

PHP method to find the location of session storage file

括号嵌套问题(建议收藏)

资深报表开发经验总结:明白这一点,没有做不好的报表

构建关系抽取的动词源

zkEVM:MINA的CEO对zkEVM和L1相关内容的总结

Go Plus Security:一款Build Web3不可或缺的安全生态基础设施

2021 CIKM |GF-VAE: A Flow-based Variational Autoencoder for Molecule Generation

Realization of online shopping mall system based on JSP
随机推荐
如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?
[unity3d shader] character projection and reflection
laravel8 实现接口鉴权封装使用JWT
One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
【云原生之kubernetes】kubernetes集群下ConfigMap使用方法
Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree
微信小程序实现音乐播放器(5)
day03_ 1_ Idea tutorial
(翻译)按钮位置约定能强化用户使用习惯
Worked overtime for a week to develop a reporting system. This low code free it reporting artifact is very easy to use
Testing is not valued? Senior: you should think in another position
UFS Clk Gate介绍
JS upload avatar (you can understand it after reading it, trust me)
Summary of senior report development experience: understand this and do not make bad reports
FPS game reverse - box Perspective (matrix)
Verilog implementation of key dithering elimination
booking.com缤客上海面经
Synchronous FIFO based on shift register
Find My技术|物联网资产跟踪市场规模达66亿美元,Find My助力市场发展
JS Base64 encoding and decoding