当前位置:网站首页>牛客-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;
}
}
边栏推荐
- Differences between seaslog and monolog log systems, installation steps of seaslog [easy to understand]
- Open source SPL enhances mangodb computing
- leetcode-6130:设计数字容器系统
- [today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
- [today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
- 程序的编译和运行
- leetcode-146:LRU 缓存
- Fanoutexchange switch code tutorial
- PayPal PHP product trial period "recommended collection"
- Step num problem
猜你喜欢

Kubernetes进阶部分学习笔记

leetcode-155:最小栈

文件操作详解

【ONNX】pytorch模型导出成ONNX格式:支持多参数与动态输入

网络协议:TCP Part2
![[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
![[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging](/img/e9/5751dc435cfbbefc22d84fd9ebbaea.png)
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging

Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
![[onnx] export pytorch model to onnx format: support multi parameter and dynamic input](/img/bd/e9a1d3a2c9343b75dbae5c7e18a87b.png)
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input

leetcode-6131:不可能得到的最短骰子序列
随机推荐
Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
智能电子界桩自然保护区远程监控解决方案
Introduction to MySQL engine and InnoDB logical storage structure
JS scope and scope chain
【单细胞高级绘图】07.KEGG富集结果展示
Increase swap space
[advanced mathematics] [5] definite integral and its application
Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!
Explain in detail the principle of MySQL master-slave replication "suggestions collection"
Yolov7 training error indexerror: list index out of range
process.env
Leetcode-146: LRU cache
火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
Leetcode-6130: designing digital container systems
leetcode-114:二叉树展开为链表
leetcode-6126:设计食物评分系统
2022.7.24-----leetcode.1184
[matlab] download originality documents based on oil monkey script and MATLAB
[leetcode] 28. Implement strstr ()
Mobile web layout method