当前位置:网站首页>牛客-TOP101-BM37
牛客-TOP101-BM37
2022-07-25 20:43:00 【一条吃猫的鱼】
import java.util.*;
/* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */
public class Solution {
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @param p int整型 * @param q int整型 * @return int整型 */
public int lowestCommonAncestor (TreeNode root, int p, int q) {
// write code here
ArrayList<Integer> pp = getPath(root, p);
ArrayList<Integer> qq = getPath(root, q);
int res = 0;
for(int i = 0 ; i < pp.size() && i < qq.size(); i++){
int x = pp.get(i);
int y = qq.get(i);
if(x == y)
res = pp.get(i);
else
break;
}
return res;
}
public ArrayList<Integer> getPath(TreeNode root, int target) {
ArrayList<Integer> list = new ArrayList<>();
TreeNode node = root;
while(node.val != target && node != null){
list.add(node.val);
if(node.val > target){
node = node.left;
}else{
node = node.right;
}
}
list.add(node.val);
return list;
}
}
边栏推荐
- Fanoutexchange switch code tutorial
- [today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
- Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
- 增加 swap 空间
- How to obtain the subordinate / annotation information of KEGG channel
- Working principle of radar water level gauge and precautions for installation and maintenance
- Character function and string function (2)
- 第六章 修改规范(SPEC)类
- Using the OAP aspect causes the controller to be called repeatedly
- [workplace rules] it workplace rules | poor performance
猜你喜欢

Google guava is just a brother. What is the real king of caching? (glory Collection Edition)
![[today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace](/img/86/abeb82927803712a98d2018421c3a7.png)
[today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
![[today in history] July 1: the father of time-sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world](/img/41/76687ea13e1722654b235f2cfa66ce.png)
[today in history] July 1: the father of time-sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world
![[advanced drawing of single cell] 07. Display of KEGG enrichment results](/img/60/09c5f44d64b96c6e4d57e5f426e4ed.png)
[advanced drawing of single cell] 07. Display of KEGG enrichment results

Remote—基本原理介绍

Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)

How to obtain the subordinate / annotation information of KEGG channel

FanoutExchange交换机代码教程

leetcode-6130:设计数字容器系统

matlab----EEGLab查看脑电信号
随机推荐
476-82(322、64、2、46、62、114)
Leetcode-6131: the shortest dice sequence impossible to get
Rand1 generates rand9
Differences between seaslog and monolog log systems, installation steps of seaslog [easy to understand]
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
Kubernetes advanced part learning notes
智能电子界桩自然保护区远程监控解决方案
【TensorRT】trtexec工具转engine
Remote - actual combat
When MySQL resets the root password and modifies the password, an error occurs. The password field does not exist
Behind every piece of information you collect, you can't live without TA
[advanced mathematics] [3] Application of differential mean value theorem and derivative
How to obtain the subordinate / annotation information of KEGG channel
【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入
[noi simulation] string matching (suffix automata Sam, Mo team, block)
增加 swap 空间
KEGG通路的从属/注释信息如何获取
MySQL inserts three tables with different values. The association condition is the primary foreign key. How about the syntax of the insertion statement?
LeetCode通关:哈希表六连,这个还真有点简单
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger