当前位置:网站首页>[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;
}
}
边栏推荐
- 常用消息队列有哪些?
- 404 not found service cannot be reached in SAP WebService test
- Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
- zabbix_get测试数据库失败
- The navigation bar changes colors according to the route
- Flink SQL 实现读写redis,并动态生成Hset key
- 微信小程序蓝牙连接硬件设备并进行通讯,小程序蓝牙因距离异常断开自动重连,js实现crc校验位
- SAP ABAP BDC (batch data communication) -018
- Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)
- 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
猜你喜欢

5. 数据访问 - EntityFramework集成

Sidecar mode

Modes of optical fiber - single mode and multimode

Randomly generate session_ id

Digital innovation driven guide

JVM the truth you need to know

往图片添加椒盐噪声或高斯噪声

Three level menu data implementation, nested three-level menu data

2pc of distributed transaction solution

R语言【逻辑控制】【数学运算】
随机推荐
CVE-2021-3156 漏洞复现笔记
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
Différenciation et introduction des services groupés, distribués et microservices
Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)
Web authentication API compatible version information
判断文件是否为DICOM文件
关于服装ERP,你知道多少?
Paper reading [MM21 pre training for video understanding challenge:video captioning with pre training techniqu]
Introduction to distributed transactions
Pinduoduo product details interface, pinduoduo product basic information, pinduoduo product attribute interface
谈fpga和asic的区别
I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
5阶多项式轨迹
《ClickHouse原理解析与应用实践》读书笔记(6)
bat 批示处理详解
Type de texte de commutation d'entrée et de mot de passe de l'applet natif
4. Object mapping Mapster
Mybaits multi table query (joint query, nested query)
C nullable type
Unity keeps the camera behind and above the player