当前位置:网站首页>[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;
}
}
边栏推荐
- 数据中心为什么需要一套基础设施可视化管理系统
- 拼多多商品详情接口、拼多多商品基本信息、拼多多商品属性接口
- Preliminary practice of niuke.com (9)
- Red Hat安装内核头文件
- The 2022 China low / no code Market Research and model selection evaluation report was released
- 随机生成session_id
- Leakage relay llj-100fs
- [论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training
- Three level menu data implementation, nested three-level menu data
- C nullable type
猜你喜欢
![[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training](/img/d6/e6db0d76e81e49a83a30f8c1832f09.png)
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training

What is message queuing?
上海字节面试问题及薪资福利

Flink SQL 实现读写redis,并动态生成Hset key

软件测试面试技巧

Realize GDB remote debugging function between different network segments

Dynamic memory management

数据中心为什么需要一套基础设施可视化管理系统

What is dependency injection (DI)
![[binary tree] binary tree path finding](/img/34/1798111e9a294b025806a4d2d5abf8.png)
[binary tree] binary tree path finding
随机推荐
R language [logic control] [mathematical operation]
An example of multi module collaboration based on NCF
nodejs获取客户端ip
OpenSergo 即将发布 v1alpha1,丰富全链路异构架构的服务治理能力
消息队列:消息积压如何处理?
How does mapbox switch markup languages?
毕业之后才知道的——知网查重原理以及降重举例
关于服装ERP,你知道多少?
CVE-2021-3156 漏洞复现笔记
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
980. 不同路径 III DFS
C nullable type
[论文阅读] Semi-supervised Left Atrium Segmentation with Mutual Consistency Training
AI face editor makes Lena smile
Paper reading [semantic tag enlarged xlnv model for video captioning]
原生小程序 之 input切換 text與password類型
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
R语言【逻辑控制】【数学运算】
京东商品详情页API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东详情API接口,京东SKU信息接口
404 not found service cannot be reached in SAP WebService test