当前位置:网站首页>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;
}
}
边栏推荐
- 95后CV工程师晒出工资单,狠补了这个,真香...
- 《编程之美》读书笔记
- devkit入门
- XML配置文件
- Date类中日期转成指定字符串出现的问题及解决方法
- Spark SQL null value, Nan judgment and processing
- How to use the flutter framework to develop and run small programs
- CTF daily question day44 rot
- How to make your own robot
- Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
猜你喜欢

Idea远程提交spark任务到yarn集群

uniapp开发,打包成H5部署到服务器
![[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)](/img/d4/4a33e7f077db4d135c8f38d4af57fa.jpg)
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)

如何利用Flutter框架开发运行小程序

如何制作自己的机器人

esxi的安装和使用

After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?

Leetcode:20220213 week race (less bugs, top 10% 555)

95后CV工程师晒出工资单,狠补了这个,真香...

数据分析思维分析方法和业务知识——分析方法(二)
随机推荐
Overview of Zhuhai purification laboratory construction details
Spark DF增加一列
Leetcode Fibonacci sequence
孤勇者
[groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
看抖音直播Beyond演唱会有感
激动人心,2022开放原子全球开源峰会报名火热开启
NLP basic task word segmentation third party Library: ICTCLAS [the third party library with the highest accuracy of Chinese word segmentation] [Chinese Academy of Sciences] [charge]
The growth path of test / development programmers, the problem of thinking about the overall situation
详细页返回列表保留原来滚动条所在位置
Dynamic programming -- linear DP
KDD 2022 | 脑电AI助力癫痫疾病诊断
Programmer growth Chapter 9: precautions in real projects
curlpost-php
Pointer - character pointer
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
Date类中日期转成指定字符串出现的问题及解决方法
XML Configuration File