当前位置:网站首页>The nearest common ancestor of 235 binary search tree
The nearest common ancestor of 235 binary search tree
2022-06-11 06:28:00 【Saucey_ six】
subject :
Their thinking : recursive , Two points of binary search tree
Binary search tree : Binary search tree , The main features are , The root node is larger than the left child , Younger than the right child , In the middle of the left small right big root .
/**
* 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;
};
Of course, the original idea is not mine , This is easier to understand .
notes : Pay attention to the second special case , So in the first one if Processing in judgment .
My initial idea was to do two iterations , It is troublesome and difficult to understand , But it can also ac.(ps: The first day )
边栏推荐
- 235-二叉搜索树的最近公共祖先
- Mediaextractor source code analysis of multimedia framework analysis (1)
- Detailed installation instructions for MySQL
- Alias the path with the help of craco
- 学好C语言从关键字开始
- 箭头函数的this指向
- [reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)
- FPGA设计——乒乓操作实现与modelsim仿真
- 不引入第三个变量,交换两个值
- Wechat applet (authorized login of TP5)
猜你喜欢

Scripy web crawler series tutorials (I) | construction of scripy crawler framework development environment

学好C语言从关键字开始

Solve the problem that ffmpeg obtains aac audio files with incorrect duration

FPGA interview topic notes (I) - FPGA development process, metastable state and competitive risk, build and hold time, asynchronous FIFO depth, etc

Metasploitabile2 target learning

MATLAB realizes mean filtering and FPGA for comparison, and uses Modelsim waveform simulation

Sqli-labs less-01

A piece of code has been refactored six times by the boss, and my mind is broken

FPGA Design -- ping pong operation implementation and Modelsim simulation
![Chapter 4 of machine learning [series] naive Bayesian model](/img/77/7720afe4e28cd55284bb365a16ba62.jpg)
Chapter 4 of machine learning [series] naive Bayesian model
随机推荐
CCS安装编译器的方法
jenkins-用户权限管理
Error reporting injection of SQL injection
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
Using Metasploit Trojan horse for remote control
Count the time-consuming duration of an operation (function)
Examinelistactivity of Shandong University project training
Review Servlet
[TP5 online export picture generation excel detailed explanation example]
箭头函数的this指向
break,continue有什么区别和用法?
Chapter 2 of machine learning [series] logistic regression model
Instanceof and type conversion
Stock K-line drawing
Vulhub 8.1-backdoor vulnerability recurrence
FPGA interview notes (III) -- implementation of handshake signal synchronization in cross clock domain, arbitrary frequency division, binary conversion, RAM memory, original code inversion and complem
[reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)
call和apply和bind的区别
Résoudre le problème de la durée inexacte du fichier audio AAC obtenu par ffmpeg
Login and registration based on servlet, JSP and MySQL