当前位置:网站首页>[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;
}
}
边栏推荐
- Get the way to optimize the one-stop worktable of customer service
- 集群、分布式、微服务的区别和介绍
- Go language context explanation
- Différenciation et introduction des services groupés, distribués et microservices
- [binary tree] binary tree path finding
- Five core elements of architecture design
- Bat instruction processing details
- JSP setting header information export to excel
- sql优化常用技巧及理解
- Go 語言的 Context 詳解
猜你喜欢
SQLSTATE[HY000][1130] Host ‘host. docker. internal‘ is not allowed to connect to this MySQL server
PowerPivot——DAX(函数)
The year of the tiger is coming. Come and make a wish. I heard that the wish will come true
分布式全局ID生成方案
Mapbox Chinese map address
Simple case of SSM framework
目标检测中的BBox 回归损失函数-L2,smooth L1,IoU,GIoU,DIoU,CIoU,Focal-EIoU,Alpha-IoU,SIoU
sql查询:将下一行减去上一行,并做相应的计算
404 not found service cannot be reached in SAP WebService test
How does mapbox switch markup languages?
随机推荐
数字IC面试总结(大厂面试经验分享)
[binary tree] binary tree path finding
zabbix_get测试数据库失败
Tablayout modification of customized tab title does not take effect
微信小程序蓝牙连接硬件设备并进行通讯,小程序蓝牙因距离异常断开自动重连,js实现crc校验位
爬虫练习题(三)
京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口
[reading of the paper] a multi branch hybrid transformer network for channel terminal cell segmentation
C#可空类型
集群、分布式、微服务的区别和介绍
原生小程序 之 input切换 text与password类型
Type de texte de commutation d'entrée et de mot de passe de l'applet natif
async / await
zabbix_ Get test database failed
PowerPivot——DAX(函数)
Taobao Commodity details page API interface, Taobao Commodity List API interface, Taobao Commodity sales API interface, Taobao app details API interface, Taobao details API interface
pytorch_ 01 automatic derivation mechanism
SAP ABAP BDC(批量数据通信)-018
Go 语言的 Context 详解
JVM the truth you need to know