当前位置:网站首页>[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;
}
}
边栏推荐
- Mybaits multi table query (joint query, nested query)
- Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
- Web architecture design process
- Différenciation et introduction des services groupés, distribués et microservices
- 架构设计的五个核心要素
- Hcip seventh operation
- 不同网段之间实现GDB远程调试功能
- What is message queuing?
- Taobao store release API interface (New), Taobao oauth2.0 store commodity API interface, Taobao commodity release API interface, Taobao commodity launch API interface, a complete set of launch store i
- 线性回归
猜你喜欢
SAP ABAP BDC(批量数据通信)-018
【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
产业金融3.0:“疏通血管”的金融科技
SQL query: subtract the previous row from the next row and make corresponding calculations
Flink SQL 实现读写redis,并动态生成Hset key
[reading of the paper] a multi branch hybrid transformer network for channel terminal cell segmentation
R语言【逻辑控制】【数学运算】
分布式事务介绍
sql查询:将下一行减去上一行,并做相应的计算
What is message queuing?
随机推荐
线性回归
zabbix_ Get test database failed
The 2022 China low / no code Market Research and model selection evaluation report was released
原生小程序 之 input切换 text与password类型
SAP ABAP BDC (batch data communication) -018
消息队列:如何确保消息不会丢失
不同网段之间实现GDB远程调试功能
OpenSergo 即将发布 v1alpha1,丰富全链路异构架构的服务治理能力
Taobao store release API interface (New), Taobao oauth2.0 store commodity API interface, Taobao commodity release API interface, Taobao commodity launch API interface, a complete set of launch store i
得物客服一站式工作台卡顿优化之路
Tablayout modification of customized tab title does not take effect
Digital innovation driven guide
Lombok plug-in
SAP webservice 测试出现404 Not found Service cannot be reached
make makefile cmake qmake都是什么,有什么区别?
Input of native applet switches between text and password types
毕业之后才知道的——知网查重原理以及降重举例
爬虫练习题(三)
《HarmonyOS实战—入门到开发,浅析原子化服务》
盘点国内有哪些EDA公司?