当前位置:网站首页>test about BinaryTree
test about BinaryTree
2022-07-06 10:53:00 【Frank.Ren】
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean getPath(TreeNode root, TreeNode node, Stack<TreeNode> stack) {
if (root == null || node == null) {
return false;
}
stack.push(root);
if (root == node) {
return true;
}
boolean key = getPath(root.left, node, stack);
if (key) {
return true;
}
key = getPath(root.right, node, stack);
if (key) {
return true;
}
stack.pop();
return false;
}
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
Stack<TreeNode> stack1 = new Stack<>();
getPath(root, p, stack1);
Stack<TreeNode> stack2 = new Stack<>();
getPath(root, q, stack2);
int diff = stack1.size() - stack2.size();
while (diff > 0) {
diff--;
stack1.pop();
}
while (diff < 0) {
diff++;
stack2.pop();
}
while (stack1.size() > 0) {
TreeNode cur = stack1.pop();
if (Objects.equals(cur, stack2.pop())) {
return cur;
}
}
return null;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
if (root == p || root == q) {
return root;
}
TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
if (leftNode != null && rightNode != null) {
return root;
} else if (leftNode != null) {
return leftNode;
} else if (rightNode != null) {
return rightNode;
}
return null;
}
}
边栏推荐
- Markdown syntax for document editing (typera)
- Splay
- celery最佳实践
- Penetration test information collection - basic enterprise information
- 手写一个的在线聊天系统(原理篇1)
- UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability
- Solve DoS attack production cases
- Picture zoom Center
- Epoll () whether it involves wait queue analysis
- 44 colleges and universities were selected! Publicity of distributed intelligent computing project list
猜你喜欢
Nuc11 cheetah Canyon setting U disk startup
On AAE
Maixll-Dock 摄像头使用
Blue Bridge Cup real question: one question with clear code, master three codes
深度循环网络长期血压预测【翻译】
The role of applet in industrial Internet
AvL树的实现
Handwritten online chat system (principle part 1)
Optical blood pressure estimation based on PPG and FFT neural network [translation]
Solve DoS attack production cases
随机推荐
Wchars, coding, standards and portability - wchars, encodings, standards and portability
人体骨骼点检测:自顶向下(部分理论)
Penetration test information collection - WAF identification
Shangsilicon Valley JUC high concurrency programming learning notes (3) multi thread lock
Breadth first traversal of graph
涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
根据PPG估算血压利用频谱谱-时间深度神经网络【翻】
AFNetworking框架_上传文件或图像server
Stm32+hc05 serial port Bluetooth design simple Bluetooth speaker
win10系统下插入U盘有声音提示却不显示盘符
Huawei 0 foundation - image sorting
十、进程管理
Crawling data encounters single point login problem
JDBC驱动器、C3P0、Druid和JDBCTemplate相关依赖jar包
DOM Brief
Automatic reservation of air tickets in C language
Test 123
Markdown syntax for document editing (typera)
同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
STM32+ESP8266+MQTT协议连接OneNet物联网平台