当前位置:网站首页>The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees
The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees
2022-08-01 05:07:00 【The harder you work, the luckier you are】
In a binary search tree:
1. If the left subtree of any node is not empty, the value of all nodes on the left subtree is not greater than the value of its root node.
2. If the right subtree of any node is not empty, the value of all nodes in the right subtree is not less than the value of its root node.
3. The left and right subtrees of any node are also binary search trees.
ie any node, left
Ancestor definition: If node p is in the left (right) subtree of node node, or p=node, then node is said to be the ancestor of p.
Definition of the nearest common ancestor: Let node node be a common ancestor of nodes p, q, if its left child node node.left and right child node.right are not p,qcommon ancestor, then node is called the "nearest common ancestor".
According to the above definition, if node is the nearest common ancestor of p,q, it can only be one of the following:
1. p and q are in the subtree of node, and the different sides of the node are divided (that is, in the left and right subtrees respectively);
2. p = node, and q is on the left or the left of node.In the right subtree;
3. q = node, and p is in the left or right subtree of node;
This question gives two important conditions: ① the tree is a binary search tree, ② the values of all nodes of the tree are unique.According to the above conditions, the subtree relationship between p, q and node can be easily judged, namely:
If node.val < p.val, then pp is in the right subtree of node;
If node.val > p.val, then pp is in the left subtree of node;
If node.val =p.val , then p and node point to the same node.
So starting from the root node, if p and q are in the left subtree of node, iterate (or search) the left subtree of node,
P and q are in the right subtree of node to iterate (or search) the right subtree of node,
Otherwise return node
Method 1: Iterate
Loop search: Jump out when the node node is empty;
When both p and q are in the right subtree of node, then traverse to node.right;
Otherwise, whenIf both p and q are in the left subtree of node, then traverse to node.left;
Otherwise, it means that the nearest common ancestor has been found, and jump out.
Return value: The nearest common ancestor node.
class Solution:def lowestCommonAncestor(self, root, p, q):while root:if root.val>p.val and root.val>q.val:root=root.leftelif root.val
class Solution:def lowestCommonAncestor(self, root, p, q):while root:if root.val>p.val and root.val>q.val:root=root.leftelif root.val
Method 2: Recursion
Recursion work:
When both p, q are in the right subtree of node, open recursion node.right and return;
otherwise, when both p and q are in the left subtree of noed, open recursion noed.left and return;
Termination condition: that is, none of the returned nodes node
Return value: The nearest common ancestor node.
class Solution:def lowestCommonAncestor(self, root, p, q):def dfs(root,p,q):if root.val > p.val and root.val > q.val:return dfs(root.left,p,q)if root.val < p.val and root.val < q.val:return dfs(root.right,p,q)return rootreturn dfs(root,p,q)class Solution:def lowestCommonAncestor(self, root, p, q):if root.val > p.val and root.val > q.val:return self.lowestCommonAncestor(root.left,p,q)if root.val < p.val and root.val < q.val:returnself.lowestCommonAncestor(root.right,p,q)return root
边栏推荐
- Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation
- typescript24-类型推论
- (2022牛客多校四)D-Jobs (Easy Version)(三维前缀或)
- (2022 Nioke Duo School IV) H-Wall Builder II (Thinking)
- Mysql基础篇(Mysql数据类型)
- pytroch、tensorflow对比学习—搭建模型范式(构建模型方法、训练模型范式)
- Error: AttributeError: module 'matplotlib' has no attribute 'figure'
- Selenium:简介
- typescript21 - Comparison of Interfaces and Type Aliases
- Robot_Framework:断言
猜你喜欢
scheduleWithFixedDelay和scheduleAtFixedRate的区别
The method of solving stored procedure table name passing through variable in mysql
Typescript20 - interface
Swastika line-by-line parsing and realization of the Transformer, and German translation practice (a)
华为Android开发面试后得出的面试秘诀
Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation
High Numbers | 【Re-integration】Line Area Score 880 Examples
备战金九银十,如何顺利通过互联网大厂Android的笔面试?
状态压缩dp
风险策略调优中重要的三步分析法
随机推荐
请问shake数据库中想把源的db0的数据同步到目的db5,参数怎么设置呢?
Selenium:表单切换
pytroch、tensorflow对比学习—搭建模型范式(构建模型方法、训练模型范式)
Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记
Selenium:元素等待
Selenium:元素定位
Selenium:上传、下载文件
PMP 项目资源管理
typescript27 - what about enumeration types
(2022牛客多校四)D-Jobs (Easy Version)(三维前缀或)
万字逐行解析与实现Transformer,并进行德译英实战(二)
Risk strategy important steps of tuning method
typescript22-接口继承
Typescript20 - interface
Pyspark机器学习:向量及其常用操作
FFmpeg 搭建本地屏幕录制环境
typescript24 - type inference
PAT乙级 1001 害死人不偿命的(3n+1)猜想
文件的异步读写
(2022牛客多校四)N-Particle Arts(思维)