当前位置:网站首页>牛客-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;
}
}
边栏推荐
- Embedded development: embedded foundation -- threads and tasks
- [advanced drawing of single cell] 07. Display of KEGG enrichment results
- Working principle of radar water level gauge and precautions for installation and maintenance
- Mobile web layout method
- [matlab] download originality documents based on oil monkey script and MATLAB
- [today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born
- “链”接无限可能:数字资产链,精彩马上来!
- leetcode-6130:设计数字容器系统
- [onnx] export pytorch model to onnx format: support multi parameter and dynamic input
- 文件操作详解
猜你喜欢

Leetcode-6125: equal row and column pairs

103. (cesium chapter) cesium honeycomb diagram (square)

Clickhouse notes 02 -- installation test clickvisual

Leetcode-919: complete binary tree inserter
![[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

leetcode-6127:优质数对的数目

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!

Docker 搭建 Redis Cluster集群

476-82(322、64、2、46、62、114)
![[leetcode] 28. Implement strstr ()](/img/87/0af2da458e53d31012d6535cc132bb.png)
[leetcode] 28. Implement strstr ()
随机推荐
[MCU] 51 MCU burning those things
Remote—实战
Card link
leetcode-6125:相等行列对
When MySQL resets the root password and modifies the password, an error occurs. The password field does not exist
Hello, I'd like to ask questions about C and database operation.
毕业从事弱电3个月,我为什么会选择转行网络工程师
增加 swap 空间
[today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
Leetcode-114: expand binary tree into linked list
How to obtain the subordinate / annotation information of KEGG channel
雷达水位计的工作原理及安装维护注意事项
【TensorRT】trtexec工具转engine
Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
Increase swap space
Arrow parquet
Interpretation of filter execution sequence source code in sprigboot
Vulnhub | dc: 5 | [actual combat]
476-82(322、64、2、46、62、114)
Unity VS—— VS中默认调试为启动而不是附加到Unity调试