当前位置:网站首页>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;
}
}
边栏推荐
- Spark SQL空值Null,NaN判断和处理
- Spark AQE
- NLP generation model 2017: Why are those in transformer
- View class diagram in idea
- [groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
- Idea remotely submits spark tasks to the yarn cluster
- State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
- 2022-02-13 work record -- PHP parsing rich text
- Yolov5、Pycharm、Anaconda环境安装
- Power Query数据格式的转换、拆分合并提取、删除重复项、删除错误、转置与反转、透视和逆透视
猜你喜欢
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
How to use the flutter framework to develop and run small programs
毕设-基于SSM高校学生社团管理系统
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea
Uniapp development, packaged as H5 and deployed to the server
XML配置文件
OpenCV经典100题
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
小程序技术优势与产业互联网相结合的分析
随机推荐
Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
Installation and use of esxi
curlpost-php
Reading notes of the beauty of programming
Yolov5、Pycharm、Anaconda环境安装
小程序技术优势与产业互联网相结合的分析
Synchronized and reentrantlock
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
STM32按键消抖——入门状态机思维
Model analysis of establishment time and holding time
Free chat robot API
Beginner redis
Hundreds of lines of code to implement a JSON parser
Arduino hexapod robot
STM32 key chattering elimination - entry state machine thinking
从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
可恢复保险丝特性测试
Spark SQL空值Null,NaN判断和处理
NLP generation model 2017: Why are those in transformer
FFmpeg抓取RTSP图像进行图像分析