当前位置:网站首页>[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;
}
}
边栏推荐
- 判断文件是否为DICOM文件
- Leetcode: maximum number of "balloons"
- 原生小程序 之 input切换 text与password类型
- 产业金融3.0:“疏通血管”的金融科技
- Cve-2021-3156 vulnerability recurrence notes
- Web authentication API compatible version information
- 目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
- 【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
- How does mapbox switch markup languages?
- Flink SQL realizes reading and writing redis and dynamically generates hset key
猜你喜欢

Introduction to distributed transactions

目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss

Five core elements of architecture design

Sidecar mode

《2022中国低/无代码市场研究及选型评估报告》发布

SAP webservice 测试出现404 Not found Service cannot be reached

分布式事务介绍

Distributed global ID generation scheme

京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口

I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
随机推荐
Mybaits multi table query (joint query, nested query)
数字IC面试总结(大厂面试经验分享)
集群、分布式、微服务的区别和介绍
论文阅读【Open-book Video Captioning with Retrieve-Copy-Generate Network】
【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
Reptile exercises (III)
Modes of optical fiber - single mode and multimode
线性回归
nodejs获取客户端ip
Wechat applet Bluetooth connects hardware devices and communicates. Applet Bluetooth automatically reconnects due to abnormal distance. JS realizes CRC check bit
What is message queuing?
Dynamic memory management
AI人脸编辑让Lena微笑
Tablayout modification of customized tab title does not take effect
不同网段之间实现GDB远程调试功能
Getting started with DES encryption
Différenciation et introduction des services groupés, distribués et microservices
C nullable type
Flinksql read / write PgSQL
Bat instruction processing details