当前位置:网站首页>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.valclass Solution:def lowestCommonAncestor(self, root, p, q):while root:if root.val>p.val and root.val>q.val:root=root.leftelif root.valMethod 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边栏推荐
- y83.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十四)
- (2022牛客多校四)N-Particle Arts(思维)
- 万字逐行解析与实现Transformer,并进行德译英实战(一)
- Selenium:浏览器操作
- Progressive Reconstruction of Visual Structure for Image Inpainting 论文笔记
- Pyspark机器学习:向量及其常用操作
- 数组问题之《两数之和》以及《三数之和 》
- 挑战52天背完小猪佩奇(第01天)
- PMP 项目质量管理
- In the shake database, I want to synchronize the data of the source db0 to the destination db5, how to set the parameters?
猜你喜欢

产品经理访谈 | 第五代验证码的创新与背景

(Codeforce 757) E. Bash Plays with Functions

pytorch、tensorflow对比学习—功能组件(激活函数、模型层、损失函数)

Pyspark Machine Learning: Vectors and Common Operations

typescript24-类型推论

(2022 Nioke Duo School IV) H-Wall Builder II (Thinking)

数组问题之《两数之和》以及《三数之和 》

可持久化线段树

Code Interview Guide for Programmers CD15 Generating an Array of Windowed Maximums

(2022牛客多校四)H-Wall Builder II(思维)
随机推荐
pytroch、tensorflow对比学习—专栏介绍
李迟2022年7月工作生活总结
56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)
25. Have you been asked these three common interview questions?
(2022 Nioke Duo School IV) D-Jobs (Easy Version) (3D prefix or)
PMP 相关方管理必背总结
pytorch、tensorflow对比学习—计算图和微分机制
Li Chi's work and life summary in July 2022
Robot_Framework:断言
万字逐行解析与实现Transformer,并进行德译英实战(一)
报错:AttributeError: module ‘matplotlib’ has no attribute ‘figure’
NDK does not contain any platforms问题解决
pytroch、tensorflow对比学习—使用GPU训练模型
RSA主要攻击方法
Dry goods!How to Construct SRv6-TE Performance Test Environment Using Instrumentation
2022年超全的Android面经(附含面试题|进阶资料)
剑指 Offer 68 - II. 二叉树的最近公共祖先
[MySQL] 多表查询
Pyspark机器学习:向量及其常用操作
mysql中解决存储过程表名通过变量传递的方法
https://leetcode.cn/problems/er-cha-sou-suo-shu-de-zui-jin-gong-gong-zu-xian-lcof/