当前位置:网站首页>235-二叉搜索树的最近公共祖先
235-二叉搜索树的最近公共祖先
2022-06-11 06:23:00 【Saucey_6】
题目:
解题思路:递归,二叉搜索树两个点
二叉搜索树:二叉查找树,主要特点是,根节点比左孩子大,比右孩子小,即左小右大根中间。
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, p, q) {
var res;
result(root, p, q);
function result(root, p, q) {
if ((root.val - p.val) * (root.val - q.val) <=0) {
res = root
} else if (root.val < p.val && root.val < q.val) {
result(root.right, p, q)
} else result(root.left, p, q)
}
return res;
};
当然最初的思路不是我的,这种比较好理解。
注:注意第二种特殊情况,所以在第一个if判断中进行处理。
我最初的思路是进行两次遍历,比较麻烦并且难理解,但是也可以ac。(ps:第一天)
边栏推荐
- Sign for this "plug-in" before returning home for the new year
- Teach everyone how to implement an electronic signature
- Sqli-labs less-01
- Quantitative understanding (Quantitative deep revolutionary networks for effective information: a whitepaper)
- break,continue有什么区别和用法?
- Chapter 4 of machine learning [series] naive Bayesian model
- 通过两种方式手写一个消息队列
- Shandong University machine learning experiment 5 SVM
- Record the first data preprocessing process
- Shandong University machine learning experiment 7 pca+ SVM face recognition
猜你喜欢

What is sentinel produced by Ali?

Zvuldrill installation and customs clearance tutorial

347. top k high frequency elements

EasyGBS接入的设备视频直播突然全部无法播放是为什么?数据库读写不够

Graphsage paper reading

autojs,读取一行删除一行,停止自己外的脚本

Metasploitabile2 target learning

Sign for this "plug-in" before returning home for the new year

FPGA面试题目笔记(二)——同步异步D触发器、静动态时序分析、分频设计、Retiming

Quantitative understanding (Quantitative deep revolutionary networks for effective information: a whitepaper)
随机推荐
Learn a trick to use MySQL functions to realize data desensitization
Free get | full function version of version control software
山东大学项目实训之examineListActivity
Verilog realizes binocular camera image data acquisition and Modelsim simulation, and finally matlab performs image display
Using Internet of things technology to accelerate digital transformation
通过两种方式手写一个消息队列
FPGA面试题目笔记(四)—— 序列检测器、跨时钟域中的格雷码、乒乓操作、降低静动态损耗、定点化无损误差、恢复时间和移除时间
Zvuldrill installation and customs clearance tutorial
Simple understanding of pseudo elements before and after
Thymeleafengine template engine
Servlet
How to use the markdown editor
C language war "minesweeping"
FPGA面试题目笔记(一)——FPGA开发流程、亚稳态和竞争冒险、建立保持时间、异步FIFO深度等
Build the first power cloud platform
FPGA interview notes (IV) -- sequence detector, gray code in cross clock domain, ping-pong operation, static and dynamic loss reduction, fixed-point lossless error, recovery time and removal time
MATLAB realizes mean filtering and FPGA for comparison, and uses Modelsim waveform simulation
Teach everyone how to implement an electronic signature
Eureka cluster setup
Review XML and JSON