当前位置:网站首页>Leetcode skimming: binary tree 25 (the nearest common ancestor of binary search tree)
Leetcode skimming: binary tree 25 (the nearest common ancestor of binary search tree)
2022-07-07 12:47:00 【Taotao can't learn English】
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 .
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 ).”
for example , Given the following binary search tree : root = [6,2,8,0,4,7,9,null,null,3,5]
Example 1:
- Input : root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
- Output : 6
- explain : node 2 And nodes 8 The most recent public ancestor of 6.
Example 2:
- Input : root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
- Output : 2
- explain : node 2 And nodes 4 The most recent public ancestor of 2, Because by definition, the nearest common ancestor node can be the node itself .
This problem can also be done by using the general method in my last section , It's just a binary search tree , Orderly , It can be done according to the nature . nature : Orderly ( The decreasing )
package com.programmercarl.tree;
/** * @ClassName BstLowestCommonAncestor * @Descriotion TODO * @Author nitaotao * @Date 2022/7/6 12:36 * @Version 1.0 * https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-search-tree/ * 235. The nearest common ancestor of a binary search tree **/
public class BstLowestCommonAncestor {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// Because it's orderly , Just find the node closest to the two elements
// The template node is on the left of the current node
if (root.val > p.val && root.val > q.val) {
root = root.left;
} else if (root.val < p.val && root.val < q.val) {
root = root.right;
} else {
//root Between the two nodes , It's a closed interval . And it's [p,q] The closed interval of , No p Namely q
// Because it moves step by step , So it's the critical point
return root;
}
return lowestCommonAncestor(root, p, q);
}
}
边栏推荐
- [疑难杂症]pip运行突然出现ModuleNotFoundError: No module named ‘pip‘
- Day-19 IO stream
- Configure an encrypted web server
- How to use PS link layer and shortcut keys, and how to do PS layer link
- test
- Multi row and multi column flex layout
- [statistical learning methods] learning notes - Chapter 5: Decision Tree
- 【PyTorch实战】用RNN写诗
- Several ways to clear floating
- What is an esp/msr partition and how to create an esp/msr partition
猜你喜欢
Day-15 common APIs and exception mechanisms
How to apply @transactional transaction annotation to perfection?
[statistical learning method] learning notes - support vector machine (Part 2)
[statistical learning method] learning notes - support vector machine (I)
Static comprehensive experiment
图形对象的创建与赋值
SQL Lab (41~45) (continuous update later)
静态Vxlan 配置
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
随机推荐
3D content generation based on nerf
Aike AI frontier promotion (7.7)
数据库安全的重要性
"Series after reading" my God! It's so simple to understand throttling and anti shake~
The IDM server response shows that you do not have permission to download the solution tutorial
【PyTorch实战】用RNN写诗
[learn microservices from 0] [03] explore the microservice architecture
test
2022 polymerization process test question simulation test question bank and online simulation test
【深度学习】图像多标签分类任务,百度PaddleClas
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
Utiliser la pile pour convertir le binaire en décimal
爱可可AI前沿推介(7.7)
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
编译 libssl 报错
JS to convert array to tree data
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
通讯协议设计与实现
SQL Lab (41~45) (continuous update later)
HZOJ #240. 图形打印四