当前位置:网站首页>236. The binary tree in recent common ancestor
236. The binary tree in recent common ancestor
2022-08-03 02:37:00 【ZNineSun】
打卡!!!每日一题
Today I bring you a tree type topic,Is also a more classic depth-first traversal
题目描述:236. 二叉树的最近公共祖先
给定一个二叉树, 找到该树中两个指定节点的最近公共祖先.
百度百科中最近公共祖先的定义为:“对于有根树 T 的两个节点 p、q,最近公共祖先表示为一个节点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先).”
题目示例:
The topics can be roughly divided into the following situations:
- If any one of the two nodes are the root node,Then we can directly return to the root node
- If two nodes are on both sides of the tree,then their nearest common ancestor can only be the root node
- The hardest case is when two nodes are on the same side of the tree
对于p或q是根节点的情况,我们只需判断p或qIs it equal to the root node?
if (p.val == root.val || q.val == root.val) {
return root;
}
Determine if it is on both sides of the tree,We only need to divide the left and right subtrees
int rootIndex = allNodeVal.indexOf(root.val);
int pIndex = allNodeVal.indexOf(p.val);
int qIndex = allNodeVal.indexOf(q.val);
/**2.two nodes on either side of the tree**/
if (pIndex < rootIndex && qIndex > rootIndex || pIndex > rootIndex && qIndex < rootIndex) {
return root;
}
The two nodes on the same side,我们需要用一个HashTable stores all parent nodes of each node,然后获取p,qlist of parent nodes,Because we need the deepest parent node ,So we can consider using inorder traversal of the binary tree,This layer of the deepest node will be in front of the list
public void dfs(TreeNode root) {
if (root == null) return;
if (root.left != null) {
parent.put(root.left.val, root);
dfs(root.left);
}
if (root.right != null) {
parent.put(root.right.val, root);
dfs(root.right);
}
}
完整代码如下:
package com.exercise.leetecode.problem.剑指offer.前中后序遍历;
import com.exercise.leetecode.common.TreeNode;
import java.util.*;
public class 二叉树的最近公共祖先_236 {
Map<Integer, TreeNode> parent = new HashMap<Integer, TreeNode>();
List<Integer> allNodeVal = new ArrayList<>();
TreeNode r;//存放根节点
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
/**1.Indicate that one of the nodes is the root node**/
if (p.val == root.val || q.val == root.val) {
return root;
}
//Determine if two nodes are on the same side
r = root;
dfs_in(root);
root = r;
boolean sig_p = false, sig_q = false;
int rootIndex = allNodeVal.indexOf(root.val);
int pIndex = allNodeVal.indexOf(p.val);
int qIndex = allNodeVal.indexOf(q.val);
/**2.two nodes on either side of the tree**/
if (pIndex < rootIndex && qIndex > rootIndex || pIndex > rootIndex && qIndex < rootIndex) {
return root;
}
dfs(root);
List<TreeNode> pParent = new ArrayList<>();
pParent.add(p);
while (p != null) {
p = parent.get(p.val);
pParent.add(p);
}
List<Integer> qParent = new ArrayList<>();
qParent.add(q.val);
while (q != null) {
q = parent.get(q.val);
if (q != null) {
qParent.add(q.val);
}
}
for (TreeNode t : pParent) {
if (qParent.contains(t.val)) {
return t;
}
}
return null;
}
public void dfs_in(TreeNode root) {
if (root == null) {
return;
}
dfs_in(root.left);
allNodeVal.add(root.val);
dfs_in(root.right);
}
public void dfs(TreeNode root) {
if (root == null) return;
if (root.left != null) {
parent.put(root.left.val, root);
dfs(root.left);
}
if (root.right != null) {
parent.put(root.right.val, root);
dfs(root.right);
}
}
}
边栏推荐
猜你喜欢
JSP第一篇 -----JSP九大内置对象(隐式对象)和四大域对象
Brute force recursion to dynamic programming 07 (516. Longest palindrome subsequence)
Violence recursion to dynamic programming 08 (pony go chess)
全栈---JSONP
Vite教程 安装
接口流量突增,如何做好性能优化?
Jenkins汉化设置
【TypeScript笔记】01 - TS初体验 && TS常用类型
【QT】自定义工程封装成DLL并如何调用(带ui界面的)
2022-08-02:小红拿到了一个大立方体,该大立方体由1*1*1的小方块拼成,初始每个小方块都是白色。 小红可以每次选择一个小方块染成红色, 每次小红可能选择同一个小方块重复染色, 每次染色以后,
随机推荐
做快乐的事情
Greenplum database failure analysis, can not listen to the port
和睦家私有化后换帅:新风天域吴启楠任CEO 李碧菁靠边站
Jenkins汉化设置
2022/8/2 考试总结
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
【Autosar RTM】
SAP ABAP OData 服务如何支持修改(Update)操作试读版
minio 单机版安装
SAP 电商云 Spartacus UI 的持续集成 - Continous integration
稳压电源: 电路图及类型
Introduction to agile development
Vite教程 安装
DB2数据库-获取表结构异常:[jcc][t4][1065][12306][4.26.14]CharConvertionException ERRORCODE=-4220,SQLSTATE=null
Nuxt 所有页面都设置上SEO相关标签
MySQL删库不跑路
担心的事情
Day017 封装
PyCharm中常用的快捷键用法详解
7.29