当前位置:网站首页>【二叉树】从二叉树一个节点到另一个节点每一步的方向
【二叉树】从二叉树一个节点到另一个节点每一步的方向
2022-08-03 13:01:00 【豪冷啊】
0x00 题目
给你一棵 二叉树 的根节点 root
这棵二叉树总共有 n 个节点
每个节点的值为 1 到 n 中的一个整数
且互不相同
给你一个整数 startValue
表示起点节点 s 的值
和另一个不同的整数 destValue
表示终点节点 t 的值
请找到从节点 s 到节点 t 的 最短路径
并以字符串的形式返回每一步的方向
每一步用 大写 字母 ‘L’ ,‘R’ 和 ‘U’ 分别表示一种方向:
L 表示从一个节点前往它的 左孩子 节点R 表示从一个节点前往它的 右孩子 节点U 表示从一个节点前往它的 父 节点
请你返回从 s 到 t 最短路径 每一步的方向
0x01 思路
因为有 2 个目标
所以有 2 条路径
先把 2 条路径找出来
然后再找出最近的公共祖先
最后再拼接路径
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 getDirections(_ root: TreeNode?, _ startValue: Int, _ destValue: Int) -> String {
guard let root = root else { return "" }
// 第一条路径
var path1: [(TreeNode, String)] = []
// 第二条路径
var path2: [(TreeNode, String)] = []
// 结果
var res = ""
// 节点和方向
var arr: [(TreeNode, String)] = []
func dfs(_ root: TreeNode?, _ val: Int, _ type: String) {
guard let root = root else { return }
// 添加
arr.append((root, type))
if root.val == val {
if val == startValue {
path1 = Array(arr)
}else if val == destValue {
path2 = Array(arr)
}
return
}
dfs(root.left, val, "L")
dfs(root.right, val, "R")
// 往回走了,删除最后一个
arr.removeLast()
}
// 查找第一条路径
dfs(root, startValue, "")
arr.removeAll()
// 查找第二条路径
dfs(root, destValue, "")
var idx1 = 0
var idx2 = 0
let idx = min(path1.count, path2.count)
// 去掉路径中相同的前缀
while idx1 < idx && idx2 < idx {
let t1 = path1[idx1]
let t2 = path2[idx2]
if t1.0.val == t2.0.val {
idx1 += 1
idx2 += 1
}else{
break
}
}
// 第一条路径是从下往上走,所以把方向全部调整为 `U`
for _ in idx1..<path1.count {
res += "U"
}
// 拼接第二条路径
for i in idx2..<path2.count {
res += path2[i].1
}
return res
}
0x03 我的小作品
欢迎体验我的作品之一:小五笔 86 版
五笔学习好帮手~App Store 搜索即可~
边栏推荐
猜你喜欢

In order to counteract the drop in sales and explore the low-end market, Weilai's new brand products are priced as low as 100,000?
![[R] Use grafify for statistical plotting, ANOVA, intervention comparisons, and more!](/img/bd/85d0f2f42a449f3b09fe3657d845f2.png)
[R] Use grafify for statistical plotting, ANOVA, intervention comparisons, and more!
![[Microservice] Multi-level cache](/img/58/72e01c789a862c058cba58b9113272.png)
[Microservice] Multi-level cache

Basic principle of the bulk of the animation and shape the An animation tip point

便携烙铁开源系统IronOS,支持多款便携DC, QC, PD供电烙铁,支持所有智能烙铁标准功能

GameFi 行业下滑但未出局| June Report

IDEA的模板(Templates)

BOM系列之sessionStorage

An动画基础之元件的影片剪辑动画与传统补间

365天挑战LeetCode1000题——Day 048 有序队列 脑筋急转弯
随机推荐
Secure Custom Web Application Login
Jmeter use
IronOS, an open source system for portable soldering irons, supports a variety of portable DC, QC, PD powered soldering irons, and supports all standard functions of smart soldering irons
GameFi 行业下滑但未出局| June Report
Golang GMP 原理
2022 年 CISO 最关心的是什么?
PyTorch framework to train linear regression model (CPU and GPU environment)
Grafana 高可用部署最佳实践
为冲销量下探中低端市场,蔚来新品牌产品定价低至10万?
An introduction to basic tools for selecting line tools (package church)
便携烙铁开源系统IronOS,支持多款便携DC, QC, PD供电烙铁,支持所有智能烙铁标准功能
来广州找工作有一个多月了,今天终于有着落了,工资7000
云计算服务主要安全风险及应对措施初探
An animation optimization of shape tween and optimization of traditional tweening
HCIP-第十二天-MPLS+VNP
Win11怎么禁止软件后台运行?Win11系统禁止应用在后台运行的方法
An animation basic element movie clip effect
Classes and objects (upper)
Golang Mutex
查看GCC版本_qt版本