当前位置:网站首页>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;
}
}
边栏推荐
- Splay
- SQL injection Foundation
- [Sun Yat sen University] information sharing of postgraduate entrance examination and re examination
- Online notes
- Reptiles have a good time. Are you full? These three bottom lines must not be touched!
- CSRF vulnerability analysis
- JDBC驱动器、C3P0、Druid和JDBCTemplate相关依赖jar包
- Deep circulation network long-term blood pressure prediction [translation]
- Use cpolar to build a business website (1)
- Handwritten online chat system (principle part 1)
猜你喜欢
AvL树的实现
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
There is a sound prompt when inserting a USB flash disk under win10 system, but the drive letter is not displayed
涂鸦智能在香港双重主板上市:市值112亿港元 年营收3亿美元
能源行业的数字化“新”运维
pychrm社区版调用matplotlib.pyplot.imshow()函数图像不弹出的解决方法
Multithreading Basics: basic concepts of threads and creation of threads
C#/VB. Net to add text / image watermarks to PDF documents
Wx applet learning notes day01
A wearable arm device for night and sleeveless blood pressure measurement [translation]
随机推荐
Precautions for binding shortcut keys of QPushButton
Oracle advanced (IV) table connection explanation
Wx applet learning notes day01
监控界的最强王者,没有之一!
node の SQLite
Shangsilicon Valley JUC high concurrency programming learning notes (3) multi thread lock
Unity资源顺序加载的一个方法
Epoll () whether it involves wait queue analysis
三年Android开发,2022疫情期间八家大厂的Android面试经历和真题整理
上海部分招工市场对新冠阳性康复者拒绝招录
Penetration test information collection - WAF identification
[the 300th weekly match of leetcode]
安装及管理程序
一种用于夜间和无袖测量血压手臂可穿戴设备【翻译】
Bonecp uses data sources
helm部署etcd集群
二叉搜索树
When visual studio code starts, it prompts "the code installation seems to be corrupt. Please reinstall." Solution to displaying "unsupported" information in the title bar
上海部分招工市場對新冠陽性康複者拒絕招錄
渲大师携手向日葵,远控赋能云渲染及GPU算力服务