当前位置:网站首页>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;
}
}
边栏推荐
- OpenCV经典100题
- Curlpost PHP
- vSphere实现虚拟机迁移
- Keepalive component cache does not take effect
- 如何制作自己的機器人
- Programmer growth Chapter 9: precautions in real projects
- cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
- Extension and application of timestamp
- synchronized 和 ReentrantLock
- Exciting, 2022 open atom global open source summit registration is hot
猜你喜欢
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
Date类中日期转成指定字符串出现的问题及解决方法
KDD 2022 | 脑电AI助力癫痫疾病诊断
Cf:h. maximum and [bit operation practice + K operations + maximum and]
Ffmpeg captures RTSP images for image analysis
小程序技术优势与产业互联网相结合的分析
2022-02-13 work record -- PHP parsing rich text
After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
Spark AQE
随机推荐
How to use the flutter framework to develop and run small programs
Leetcode Fibonacci sequence
curlpost-php
激动人心,2022开放原子全球开源峰会报名火热开启
Power query data format conversion, Split Merge extraction, delete duplicates, delete errors, transpose and reverse, perspective and reverse perspective
Starting from 1.5, build a micro Service Framework - call chain tracking traceid
Zhuhai's waste gas treatment scheme was exposed
Cve-2017-11882 reappearance
95后CV工程师晒出工资单,狠补了这个,真香...
Exciting, 2022 open atom global open source summit registration is hot
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
The value of applet containers
数据分析思维分析方法和业务知识——分析方法(二)
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
Zhuhai laboratory ventilation system construction and installation instructions
XML Configuration File
时间戳的拓展及应用实例
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
MIT doctoral thesis | robust and reliable intelligent system using neural symbol learning