当前位置:网站首页>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
边栏推荐
- G1 garbage collector of garbage collector
- Typescript official website tutorial
- Image 24 bit depth to 8 bit depth
- What London Silver Trading software supports multiple languages
- How do microservices aggregate API documents? This wave of operation is too good
- CV in transformer learning notes (continuously updated)
- Su embedded training - Day10
- How to disable the clear button of ie10 insert text box- How can I disable the clear button that IE10 inserts into textboxes?
- What kind of experience is it when the Institute earns 20000 yuan a month?
- [combinatorics] exponential generating function (example of exponential generating function solving multiple set arrangement)
猜你喜欢

Recommend a simple browser tab

Mysql45 lecture learning notes (II)

2022-2028 global lithium battery copper foil industry research and trend analysis report

CTO and programmer were both sentenced for losing control of the crawler

Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)

Have you learned the correct expression posture of programmers on Valentine's day?

Computer graduation project PHP library book borrowing management system

Caddy server agent

What kind of experience is it when the Institute earns 20000 yuan a month?

Computer graduation design PHP sports goods online sales system website
随机推荐
[combinatorics] exponential generating function (proving that the exponential generating function solves the arrangement of multiple sets)
Enterprise custom form engine solution (12) -- form rule engine 2
Suffix derivation based on query object fields
199. Right view of binary tree - breadth search
Raft 日志复制
How to track the real-time trend of Bank of London
How about the Moco model?
Zero length array
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation)
2022-2028 global marking ink industry research and trend analysis report
Real time split network (continuous update)
Reappearance of ASPP (atlas spatial pyramid pooling) code
[combinatorics] exponential generating function (example 2 of solving multiple set permutation with exponential generating function)
22.2.14 -- station B login with code -for circular list form - 'no attribute' - 'needs to be in path selenium screenshot deviation -crop clipping error -bytesio(), etc
Bloom filter [proposed by bloom in 1970; redis cache penetration solution]
[untitled]
Reading a line from ifstream into a string variable
Shell script return value with which output
[combinatorics] exponential generating function (properties of exponential generating function | exponential generating function solving multiple set arrangement)
Sensor 调试流程