当前位置:网站首页>[binary tree] the longest interleaved path in a binary tree
[binary tree] the longest interleaved path in a binary tree
2022-07-26 04:06:00 【How cold it is】
0x00 subject
Here's one for you root A binary tree for the root , The alternate paths in a binary tree are defined as follows :
Select the binary tree arbitrarily Nodes and a direction ( Left perhaps Right )
If the forward direction is Right , Then move to the current node Right Child node
Otherwise move to its Left Child node
Change the direction : Left to right perhaps Right to left
Repeat steps 2 and 3 , Until you are in the tree unable Keep moving
The length of the interleaved path is defined as :
Visited nodes number - 1( The path length of a single node is 0 )
Please return the longest of the given tree Alternate paths The length of
0x01 Ideas
use depth Priority traversal
Traverse Next When a child node
Yes, it can distinguish the node from Left node , still Right Node
By caching for each node 2 It's worth
A left value left, Indicates that this node is The left node when , Arrive at the required Number of paths
An R-value right, Indicates that this node is Right node when , Arrive at the required Number of paths
Because nodes are either Left node , Or Right node
So two values , One must be 0
Another one , According to this node is Left yes Right To make sure
yes Left node , The parent node's Right value + 1 (rihgt + 1)
yes Right node , The parent node's The left value + 1 (left + 1)
Finally, use one overall situation Variable
Save the maximum value
0x02 solution
Language :Swift
Tree node :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
}
}
solution :
func longestZigZag(_ root: TreeNode?) -> Int {
var res: Int = 0
func dfs(_ root: TreeNode?, _ left: Int, _ right: Int) {
// Maximum length
res = max(res, max(left, right))
// The left subtree exists , To the right
if let l = root?.left {
dfs(l, right+1, 0)
}
// Right subtree exists , To the left
if let r = root?.right {
dfs(r, 0, left+1)
}
}
dfs(root, 0, 0)
return res
}
0x03 My little work
Welcome to experience one of my works : Small five strokes
Wubi learning is a good helper !App Store Search ~
边栏推荐
- Pat class a 1039 course list for student
- SDL2 Opengl遇到的坑
- What are the differences between vite and wenpack?
- [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)
- Helloworld案例分析
- laravel8 实现接口鉴权封装使用JWT
- 【读书笔记->数据分析】01 数据分析导论
- php 查找 session 存储文件位置的方法
- 微信小程序实现音乐播放器(4)(使用pubsubjs实现页面间通信)
- JS upload avatar (you can understand it after reading it, trust me)
猜你喜欢

Working ideas of stability and high availability guarantee

redux

Asemi rectifier bridge gbu1510 parameters, gbu1510 specifications, gbu1510 package

第十八章:2位a~b进制中均位奇观探索,指定整数的 3x+1 转化过程,指定区间验证角谷猜想,探求4份黑洞数,验证3位黑洞数

CPU and GPU are out of date, and the era of NPU and APU begins

《opencv学习笔记》-- 重映射

Luoda Development -- the context of sidetone configuration

Sentinel fusing and current limiting
![Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130](/img/84/e5cb5199fe4602440b50dfc4afe963.gif)
Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130

基于移位寄存器的同步FIFO
随机推荐
[question 019: what is the understanding of spherecastcommand in unity?]
PHP save array to var file_ export、serialize
PHP 对象转换数组
What are the differences between vite and wenpack?
Working ideas of stability and high availability guarantee
[cloud native] talk about the understanding of the old message middleware ActiveMQ
E-commerce operator Xiaobai, how to get started quickly and learn data analysis?
(翻译)网站流程图和用户流程图的使用时机
Oracle 11g "password delayed verification" feature
(translation) timing of website flow chart and user flow chart
(translation) the button position convention can strengthen the user's usage habits
《opencv学习笔记》-- 霍夫变换
Chinese database oceanbase was selected into the Forrester translational data platform report
测试用例设计方法之——招式组合,因果判定
JS Base64 encoding and decoding
深度学习之SuperViT
[oi knowledge] dichotomy, dichotomy concept, integer dichotomy, floating point dichotomy
PHP implements the algorithm of adding from 1 to 100
Where does international crude oil open an account, a formal, safe and secure platform
Mantium 如何在 Amazon SageMaker 上使用 DeepSpeed 实现低延迟 GPT-J 推理