当前位置:网站首页>[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;
}
}
边栏推荐
- 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
- Simple case of SSM framework
- Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
- Differences and introduction of cluster, distributed and microservice
- 分布式事务解决方案之TCC
- STM32按键状态机2——状态简化与增加长按功能
- JSP setting header information export to excel
- JVM the truth you need to know
- What are the common message queues?
- Message queuing: how to ensure that messages are not lost
猜你喜欢
【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
数据中心为什么需要一套基础设施可视化管理系统
《ClickHouse原理解析与应用实践》读书笔记(6)
Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)
Leakage relay llj-100fs
Differences and introduction of cluster, distributed and microservice
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training
消息队列:如何确保消息不会丢失
Reptile exercises (III)
sql优化常用技巧及理解
随机推荐
三级菜单数据实现,实现嵌套三级菜单数据
《HarmonyOS实战—入门到开发,浅析原子化服务》
Lombok plug-in
Win configuration PM2 boot auto start node project
Harmonyos practice - Introduction to development, analysis of atomized services
Getting started with DES encryption
2pc of distributed transaction solution
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
AI人脸编辑让Lena微笑
CVE-2021-3156 漏洞复现笔记
make makefile cmake qmake都是什么,有什么区别?
Dynamic memory management
Mysql-centos7 install MySQL through yum
Reading the paper [sensor enlarged egocentric video captioning with dynamic modal attention]
sql优化常用技巧及理解
English grammar_ Noun possessive
[reading of the paper] a multi branch hybrid transformer network for channel terminal cell segmentation
980. 不同路径 III DFS
Message queuing: how to ensure that messages are not lost
[PM products] what is cognitive load? How to adjust cognitive load reasonably?