当前位置:网站首页>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;
}
}
边栏推荐
- Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?
- [groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
- 小程序技术优势与产业互联网相结合的分析
- 数据分析思维分析方法和业务知识——分析方法(二)
- Yolov5, pychar, Anaconda environment installation
- Pointer - character pointer
- MCU realizes OTA online upgrade process through UART
- CTF daily question day44 rot
- Spark获取DataFrame中列的方式--col,$,column,apply
- 可恢复保险丝特性测试
猜你喜欢

Browser reflow and redraw

Questions about database: (5) query the barcode, location and reader number of each book in the inventory table

Spark SQL空值Null,NaN判断和处理

esxi的安装和使用
![Cf:h. maximum and [bit operation practice + K operations + maximum and]](/img/c2/9e58f18eec2ff92e164d8d156629cf.png)
Cf:h. maximum and [bit operation practice + K operations + maximum and]
![Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]](/img/9e/c933f454a39d906a407e4d415f0b87.png)
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]

关于#数据库#的问题:(5)查询库存表中每本书的条码、位置和借阅的读者编号

The third season of ape table school is about to launch, opening a new vision for developers under the wave of going to sea

2022-02-13 work record -- PHP parsing rich text

The relationship between FPGA internal hardware structure and code
随机推荐
面试必刷算法TOP101之回溯篇 TOP34
MySQL storage engine
数据分析思维分析方法和业务知识——分析方法(三)
【线上小工具】开发过程中会用到的线上小工具合集
Power Query数据格式的转换、拆分合并提取、删除重复项、删除错误、转置与反转、透视和逆透视
Reading notes of the beauty of programming
Cannot resolve symbol error
小程序容器可以发挥的价值
Hundreds of lines of code to implement a JSON parser
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
Extension and application of timestamp
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
Model analysis of establishment time and holding time
curlpost-php
What is the most suitable book for programmers to engage in open source?
看抖音直播Beyond演唱会有感
Comment faire votre propre robot
详细页返回列表保留原来滚动条所在位置
Spark SQL空值Null,NaN判断和处理
云导DNS和知识科普以及课堂笔记