当前位置:网站首页>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;
}
}
边栏推荐
- curlpost-php
- vSphere实现虚拟机迁移
- Leetcode 44 Wildcard matching (2022.02.13)
- How to make your own robot
- Classic CTF topic about FTP protocol
- Exciting, 2022 open atom global open source summit registration is hot
- Fibonacci number
- [groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
- Spark AQE
- Leetcode:20220213 week race (less bugs, top 10% 555)
猜你喜欢
Idea远程提交spark任务到yarn集群
如何制作自己的机器人
Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
What is the most suitable book for programmers to engage in open source?
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
Leetcode 450 deleting nodes in a binary search tree
Ffmpeg captures RTSP images for image analysis
如何制作自己的機器人
随机推荐
NLP generation model 2017: Why are those in transformer
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
STM32按键消抖——入门状态机思维
C language programming (Chapter 6 functions)
激动人心,2022开放原子全球开源峰会报名火热开启
Zhuhai's waste gas treatment scheme was exposed
Promise
Arduino hexapod robot
ubantu 查看cudnn和cuda的版本
时间戳的拓展及应用实例
Free chat robot API
2022-02-13 work record -- PHP parsing rich text
Opencv classic 100 questions
如何利用Flutter框架开发运行小程序
MIT doctoral thesis | robust and reliable intelligent system using neural symbol learning
State mode design procedure: Heroes in the game can rest, defend, attack normally and attack skills according to different physical strength values.
DD's command
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
【文件IO的简单实现】
XML Configuration File