当前位置:网站首页>235. The nearest common ancestor of the binary search tree [LCA template + same search path]
235. The nearest common ancestor of the binary search tree [LCA template + same search path]
2022-07-03 18:39:00 【White speed Dragon King's review】
analysis
find root To p and q The path of ( adopt while And size )
Then see where the two paths can reach the same depth
Just return to the same deepest position
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':
# find root To target Of path
def get_path(start, target):
now = start
path = []
while now != target:
path.append(now)
# Next level search
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
summary
lca Templates
边栏推荐
- Closure and closure function
- 199. Right view of binary tree - breadth search
- Class exercises
- What does foo mean in programming?
- [combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
- This diversion
- MySQL duplicate check
- Unity webgl optimization
- [leetcode周赛]第300场——6110. 网格图中递增路径的数目-较难
- Torch learning notes (1) -- 19 common ways to create tensor
猜你喜欢
2022-2028 global aircraft head up display (HUD) industry research and trend analysis report
Sensor 调试流程
Ping problem between virtual machine and development board
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
论文阅读 GloDyNE Global Topology Preserving Dynamic Network Embedding
Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
After the festival, a large number of people change careers. Is it still time to be 30? Listen to the experience of the past people
2022-2028 global physiotherapy clinic industry research and trend analysis report
Read the paper glodyne global topology preserving dynamic network embedding
[Godot] add menu button
随机推荐
How to quickly view the inheritance methods of existing models in torchvision?
Recommend a simple browser tab
[combinatorics] generating function (positive integer splitting | repeated ordered splitting | non repeated ordered splitting | proof of the number of repeated ordered splitting schemes)
English grammar_ Noun classification
Read the paper glodyne global topology preserving dynamic network embedding
How to draw non overlapping bubble chart in MATLAB
What is SQL get connection
FBI 警告:有人利用 AI 换脸冒充他人身份进行远程面试
Redis cache avalanche, penetration, breakdown
Bloom filter [proposed by bloom in 1970; redis cache penetration solution]
Win 11 major updates, new features love love.
What London Silver Trading software supports multiple languages
[combinatorics] exponential generating function (example 2 of solving multiple set permutation with exponential generating function)
[combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
Coordinate layer conversion tool (video)
How to read the source code [debug and observe the source code]
[combinatorics] exponential generating function (properties of exponential generating function | exponential generating function solving multiple set arrangement)
企业级自定义表单引擎解决方案(十二)--表单规则引擎2
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation)
Xception for deeplab v3+ (including super detailed code comments and original drawing of the paper)