当前位置:网站首页>Finding the nearest common ancestor of binary search tree by recursion
Finding the nearest common ancestor of binary search tree by recursion
2022-07-06 00:50:00 【Hydrion-Qlz】
235. The nearest common ancestor of a binary search tree
difficulty : Simple
Given a binary search tree , Find the nearest common ancestor of the two specified nodes in the tree .
Baidu Encyclopedia The definition of the most recent common ancestor in 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 ).”

Ideas
This problem must be used well BST Properties of trees !
There are three situations
- Both nodes are smaller than the current node , Are all on the left subtree of the current node
- Both nodes are larger than the current node , Are all on the right subtree of the current node
- One is larger than the current node ( Or equal to ), One is smaller than the current node ( Or equal to ), Then the current node is the nearest common ancestor of the two nodes
Using recursion is easy
package cn.edu.xjtu.carlWay.tree.commonAncestorBST;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 235. The nearest common ancestor of a binary search tree * Given a binary search tree , Find the nearest common ancestor of the two specified nodes in the tree . * <p> * 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 ). * <p> * https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ */
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// case 1
if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
// case 2
if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
// case 3
return root;
}
}
边栏推荐
- Four dimensional matrix, flip (including mirror image), rotation, world coordinates and local coordinates
- Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
- Spark获取DataFrame中列的方式--col,$,column,apply
- 详细页返回列表保留原来滚动条所在位置
- Leetcode Fibonacci sequence
- 测试/开发程序员的成长路线,全局思考问题的问题......
- curlpost-php
- Synchronized and reentrantlock
- CTF daily question day44 rot
- Comment faire votre propre robot
猜你喜欢

Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?

激动人心,2022开放原子全球开源峰会报名火热开启

程序员搞开源,读什么书最合适?

esxi的安装和使用

Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code

Study diary: February 13, 2022

Data analysis thinking analysis methods and business knowledge - analysis methods (III)

Spark SQL空值Null,NaN判断和处理
![[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte](/img/61/73becfc3b46669d31b0cf334aa54f2.jpg)
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte

cf:C. The Third Problem【关于排列这件事】
随机推荐
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
Logstash clear sincedb_ Path upload records and retransmit log data
DD's command
面试必刷算法TOP101之回溯篇 TOP34
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
[Online gadgets] a collection of online gadgets that will be used in the development process
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
《编程之美》读书笔记
Spark AQE
MDK debug时设置数据实时更新
How to make your own robot
Leetcode 44 Wildcard matching (2022.02.13)
程序员搞开源,读什么书最合适?
MCU通过UART实现OTA在线升级流程
Exciting, 2022 open atom global open source summit registration is hot
【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
【线上小工具】开发过程中会用到的线上小工具合集
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
Cf:h. maximum and [bit operation practice + K operations + maximum and]