当前位置:网站首页>Niuke-top101-bm38
Niuke-top101-bm38
2022-07-25 20:45:00 【A fish that eats cats】
import java.util.*;
/* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */
public class Solution {
/** * * @param root TreeNode class * @param o1 int integer * @param o2 int integer * @return int integer */
boolean sign = false;
public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
// write code here
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
dfs(root, o1, list1);
sign = false;
dfs(root, o2, list2);
int res = 0;
for(int i = 0; i < list1.size() && i < list2.size(); i++){
int x = list1.get(i);
int y = list2.get(i);
if(x == y){
res = list1.get(i);
}else{
break;
}
}
return res;
}
public void dfs(TreeNode root, int target, ArrayList<Integer> path){
// Sentenced to empty
if(root == null || sign == true)
return;
path.add(root.val);
if(root.val == target){
sign = true;
return;
}
//dfs Traverse
dfs(root.left, target, path);
dfs(root.right, target, path);
if(sign == true)
return;
// to flash back
path.remove(path.size() - 1);
}
}
边栏推荐
- Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework
- Remote monitoring solution of intelligent electronic boundary stake Nature Reserve
- Automated testing ----- selenium (I)
- leetcode-146:LRU 缓存
- [advanced mathematics] [8] differential equation
- leetcode-6129:全 0 子数组的数目
- Unity VS—— VS中默认调试为启动而不是附加到Unity调试
- Introduction to MySQL engine and InnoDB logical storage structure
- Network protocol: TCP part2
- Hello, I'd like to ask questions about C and database operation.
猜你喜欢

Network protocol: TCP part2

【单细胞高级绘图】07.KEGG富集结果展示

If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)

LeetCode通关:哈希表六连,这个还真有点简单
![[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger](/img/14/f2b68dbe4e6a9b8d89ed9ff38f5e11.png)
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
![[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day](/img/7d/7a01c8c6923077d6c201bf1ae02c8c.png)
[today in history] July 5: the mother of Google was born; Two Turing Award pioneers born on the same day

476-82(322、64、2、46、62、114)

Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34

leetcode-6131:不可能得到的最短骰子序列

Online random coin tossing positive and negative statistical tool
随机推荐
MySQL 日期【加号/+】条件筛选问题
Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released
Arrow parquet
Mobile web layout method
[today in history] July 15: Mozilla foundation was officially established; The first operation of Enigma cipher machine; Nintendo launches FC game console
Character function and string function (2)
Struct, enum type and union
程序的编译和运行
Fanoutexchange switch code tutorial
Use Navicat to connect to MySQL database through SSH channel (pro test is feasible)
Leetcode-79: word search
智能电子界桩自然保护区远程监控解决方案
MPI learning notes (II): two implementation methods of matrix multiplication
使用oap切面导致controller被重复调用
Leetcode-919: complete binary tree inserter
【TensorRT】trtexec工具转engine
process.env
How to choose a microservice registration center?
matlab----EEGLab查看脑电信号
[technical dry goods] how to ensure the idempotency of the interface?