当前位置:网站首页>[daily training -- Tencent selected 50] 235 Nearest common ancestor of binary search tree
[daily training -- Tencent selected 50] 235 Nearest common ancestor of binary search tree
2022-07-07 05:49:00 【Puppet__】
subject
Given a binary search tree , Find the nearest common ancestor of the two specified nodes in the tree .
In Baidu Encyclopedia, the most recent definition of public ancestor is :“ For a tree T Two nodes of p、q, Recently, the common ancestor is represented as a node x, Satisfy x yes p、q Our ancestors and x As deep as possible ( A node can also be its own ancestor ).”
for example , Given the following binary search tree : root = [6,2,8,0,4,7,9,null,null,3,5]
Example 1:
Input : root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output : 6
explain : node 2 And nodes 8 The most recent public ancestor of 6.
Example 2:
Input : root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output : 2
explain : node 2 And nodes 4 The most recent public ancestor of 2, Because by definition, the nearest common ancestor node can be the node itself .
explain :
The values of all nodes are unique .
p、q For different nodes and all exist in a given binary search tree .
Code
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */
class Solution {
// At the same time, find the arrival p、q The path of , Find the bifurcation point of two paths , Make full use of the properties of binary search tree
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode ansNode = root;
while(true){
if(p.val < ansNode.val && q.val < ansNode.val){
ansNode = ansNode.left;
}
else if(p.val > ansNode.val && q.val > ansNode.val){
ansNode = ansNode.right;
}
// The path forked , This point is the nearest common ancestor
else{
break;
}
}
return ansNode;
}
}
边栏推荐
- Différenciation et introduction des services groupés, distribués et microservices
- SQLSTATE[HY000][1130] Host ‘host. docker. internal‘ is not allowed to connect to this MySQL server
- Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
- Message queue: how to handle repeated messages?
- 随机生成session_id
- Mysql-centos7 install MySQL through yum
- Nodejs get client IP
- 消息队列:如何确保消息不会丢失
- Digital innovation driven guide
- How to get free traffic in pinduoduo new store and what links need to be optimized in order to effectively improve the free traffic in the store
猜你喜欢

《ClickHouse原理解析与应用实践》读书笔记(6)
![Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]](/img/9c/1f031400f0e201df47bd51547ff73f.png)
Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]
![[paper reading] semi supervised left atrium segmentation with mutual consistency training](/img/d6/e6db0d76e81e49a83a30f8c1832f09.png)
[paper reading] semi supervised left atrium segmentation with mutual consistency training

Unity keeps the camera behind and above the player
![[binary tree] binary tree path finding](/img/34/1798111e9a294b025806a4d2d5abf8.png)
[binary tree] binary tree path finding

4. 对象映射 - Mapping.Mapster

目标检测中的BBox 回归损失函数-L2,smooth L1,IoU,GIoU,DIoU,CIoU,Focal-EIoU,Alpha-IoU,SIoU

三级菜单数据实现,实现嵌套三级菜单数据

Leetcode: maximum number of "balloons"

An example of multi module collaboration based on NCF
随机推荐
ForkJoin最全详解(从原理设计到使用图解)
Randomly generate session_ id
EMMC打印cqhci: timeout for tag 10提示分析与解决
[paper reading] semi supervised left atrium segmentation with mutual consistency training
What is dependency injection (DI)
Nodejs get client IP
Educational Codeforces Round 22 B. The Golden Age
Modes of optical fiber - single mode and multimode
SAP ABAP BDC(批量数据通信)-018
Hcip seventh operation
Get the way to optimize the one-stop worktable of customer service
nVisual网络可视化
zabbix_ Get test database failed
【Shell】清理nohup.out文件
软件测试面试技巧
AI face editor makes Lena smile
R language [logic control] [mathematical operation]
Type de texte de commutation d'entrée et de mot de passe de l'applet natif
Différenciation et introduction des services groupés, distribués et microservices
京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口