当前位置:网站首页>test about BinaryTree
test about BinaryTree
2022-07-06 18:54: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;
}
}
边栏推荐
- 【中山大学】考研初试复试资料分享
- 同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
- 青龙面板最近的库
- Implementation of AVL tree
- 上海部分招工市場對新冠陽性康複者拒絕招錄
- 能源行业的数字化“新”运维
- Precautions for binding shortcut keys of QPushButton
- Openmv4 learning notes 1 --- one click download, background knowledge of image processing, lab brightness contrast
- If you have any problems, you can contact me. A rookie ~
- 44 colleges and universities were selected! Publicity of distributed intelligent computing project list
猜你喜欢
None of the strongest kings in the monitoring industry!
Nuc11 cheetah Canyon setting U disk startup
星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿
Three years of Android development, Android interview experience and real questions sorting of eight major manufacturers during the 2022 epidemic
基于ppg和fft神经网络的光学血压估计【翻译】
多线程基础:线程基本概念与线程的创建
pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目
Collection of penetration test information -- use with nmap and other tools
手写一个的在线聊天系统(原理篇1)
随机推荐
Master Xuan joined hands with sunflower to remotely control enabling cloud rendering and GPU computing services
[sword finger offer] 60 Points of N dice
RedisSystemException:WRONGTYPE Operation against a key holding the wrong kind of value
[depth first search] Ji suanke: find numbers
Wx applet learning notes day01
287. 寻找重复数
Some understandings of tree LSTM and DGL code implementation
Xu Xiang's wife Ying Ying responded to the "stock review": she wrote it!
R语言使用rchisq函数生成符合卡方分布的随机数、使用plot函数可视化符合卡方分布的随机数(Chi Square Distribution)
From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects
UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability
The role of applet in industrial Internet
[Matlab] Simulink 同一模块的输入输出的变量不能同名
朗坤智慧冲刺科创板:年营收4亿 拟募资7亿
About NPM install error 1
None of the strongest kings in the monitoring industry!
[the 300th weekly match of leetcode]
上海部分招工市場對新冠陽性康複者拒絕招錄
涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
Atcoder a mountaineer