当前位置:网站首页>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;
}
}
边栏推荐
- Using block to realize the traditional values between two pages
- MySQL查询请求的执行过程——底层原理
- C language college laboratory reservation registration system
- 裕太微冲刺科创板:拟募资13亿 华为与小米基金是股东
- Execution process of MySQL query request - underlying principle
- Splay
- UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability
- CSRF vulnerability analysis
- 2022/02/12
- Solve DoS attack production cases
猜你喜欢
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
一种用于夜间和无袖测量血压手臂可穿戴设备【翻译】
Introduction and case analysis of Prophet model
Coco2017 dataset usage (brief introduction)
CSRF vulnerability analysis
线代笔记....
ORACLE进阶(四)表连接讲解
Noninvasive and cuff free blood pressure measurement for telemedicine [translation]
星诺奇科技IPO被终止:曾拟募资3.5亿元 年营收3.67亿
Nuc11 cheetah Canyon setting U disk startup
随机推荐
Example of implementing web server with stm32+enc28j60+uip protocol stack
朗坤智慧冲刺科创板:年营收4亿 拟募资7亿
MySQL查询请求的执行过程——底层原理
Docker installation redis
Human bone point detection: top-down (part of the theory)
根据PPG估算血压利用频谱谱-时间深度神经网络【翻】
44 colleges and universities were selected! Publicity of distributed intelligent computing project list
具体说明 Flume介绍、安装和配置
First, look at K, an ugly number
node の SQLite
STM32+ENC28J60+UIP协议栈实现WEB服务器示例
STM32+HC05串口蓝牙设计简易的蓝牙音箱
Handwritten online chat system (principle part 1)
CRMEB 商城系统如何助力营销?
Jdbc driver, c3p0, druid and jdbctemplate dependent jar packages
Deep circulation network long-term blood pressure prediction [translation]
Celery best practices
Penetration test information collection - App information
C language college laboratory reservation registration system
小程序在产业互联网中的作用