当前位置:网站首页>235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】
235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】
2022-07-03 18:43:00 【白速龍王的回眸】

分析
找到root到p和q的路徑(通過while和大小關系即可)
然後看看兩條路徑最深能到哪裏相同
返回最深的相同的比特置即可
ac code
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
# 找到root到target的path
def get_path(start, target):
now = start
path = []
while now != target:
path.append(now)
# 下一層尋找
if target.val > now.val:
now = now.right
else:
now = now.left
path.append(target)
return path
pa = get_path(root, p)
pb = get_path(root, q)
ans = None
for u, v in zip(pa, pb):
if u == v:
ans = u
else:
break
return ans
總結
lca模板
边栏推荐
- Nodejs (01) - introductory tutorial
- Opencv learning notes (continuously updated)
- Add control at the top of compose lazycolumn
- Raft log replication
- Suffix derivation based on query object fields
- Coordinate layer conversion tool (video)
- Computer graduation design PHP sports goods online sales system website
- [combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
- leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
- Should I be laid off at the age of 40? IBM is suspected of age discrimination, calling its old employees "dinosaurs" and planning to dismiss, but the employees can't refute it
猜你喜欢

VLAN experiment

Raft log replication

How many convolution methods does deep learning have? (including drawings)
![leetcode:556. Next larger element III [simulation + change as little as possible]](/img/a0/12e5ee5d01d666acb4b75ada2e6fec.png)
leetcode:556. Next larger element III [simulation + change as little as possible]

2022.02.11

SQL injection -day16

2022-2028 global sepsis treatment drug industry research and trend analysis report

Nodejs (01) - introductory tutorial

组策略中开机脚本与登录脚本所使用的用户身份

Chisel tutorial - 06 Phased summary: implement an FIR filter (chisel implements 4-bit FIR filter and parameterized FIR filter)
随机推荐
[combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
企业级自定义表单引擎解决方案(十二)--表单规则引擎2
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation example 2 | extended to integer solution)
Torch learning notes (4) -- torch's dynamic calculation diagram
An academic paper sharing and approval system based on PHP for computer graduation design
leetcode:11. 盛最多水的容器【双指针 + 贪心 + 去除最短板】
English语法_名词 - 分类
Computer graduation project PHP library book borrowing management system
硬盘监控和分析工具:Smartctl
How to read the source code [debug and observe the source code]
Setinterval CPU intensive- Is setInterval CPU intensive?
Administrative division code acquisition
198. Looting - Dynamic Planning
Sensor 调试流程
Bidding procurement scheme management of Oracle project management system
What London Silver Trading software supports multiple languages
leetcode:11. 盛最多水的容器【雙指針 + 貪心 + 去除最短板】
Gao Qing, Beijing University of Aeronautics and Astronautics: CIM is a natural quantum computing platform for graph data processing
How do microservices aggregate API documents? This wave of operation is too good
Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)