当前位置:网站首页>leetcode 865. Smallest Subtree with all the Deepest Nodes | 865.具有所有最深节点的最小子树(树的BFS,parent反向索引map)
leetcode 865. Smallest Subtree with all the Deepest Nodes | 865.具有所有最深节点的最小子树(树的BFS,parent反向索引map)
2022-07-08 00:37:00 【寒泉Hq】
题目
https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/
题解
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public TreeNode subtreeWithAllDeepest(TreeNode root) {
// 层序遍历,记录最深层
Queue<TreeNode> preQueue = new LinkedList<>();
Queue<TreeNode> curQueue = new LinkedList<>();
Queue<TreeNode> nextQueue;
curQueue.offer(root);
while (!curQueue.isEmpty()) {
preQueue = new LinkedList<>(curQueue);
nextQueue = new LinkedList<>();
while (!curQueue.isEmpty()) {
TreeNode cur = curQueue.poll();
if (cur.left != null) {
nextQueue.offer(cur.left);
}
if (cur.right != null) {
nextQueue.offer(cur.right);
}
}
if (nextQueue.isEmpty()) {
break;
}
curQueue = nextQueue;
}
// 建立树反向索引表
HashMap<TreeNode, TreeNode> parentMap = new HashMap<>();
dfs(root, parentMap);
// 从最深层每个节点向上找parent,路过时,节点经过次数count++,找到最早出现count==n的祖先
HashMap<Integer, Integer> countMap = new HashMap<>();
int n = preQueue.size();
while (!preQueue.isEmpty()) {
TreeNode cur = preQueue.poll();
while (cur != null) {
int count = countMap.getOrDefault(cur.val, 0) + 1;
if (count == n) {
return cur;
}
countMap.put(cur.val, count);
cur = parentMap.get(cur);
}
}
return null;
}
public void dfs(TreeNode node, HashMap<TreeNode, TreeNode> map) {
if (node.left != null) {
map.put(node.left, node);
dfs(node.left, map);
}
if (node.right != null) {
map.put(node.right, node);
dfs(node.right, map);
}
}
}
边栏推荐
- 分布式定时任务之XXL-JOB
- From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
- Exit of processes and threads
- The numerical value of the number of figures thought of by the real-time update of the ranking list
- Usage of hydraulic rotary joint
- From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
- Voice of users | understanding of gbase 8A database learning
- Android 创建的sqlite3数据存放位置
- 电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
- 如何用Diffusion models做interpolation插值任务?——原理解析和代码实战
猜你喜欢
C语言-Cmake-CMakeLists.txt教程
Keras深度学习实战——基于Inception v3实现性别分类
SQLite3 data storage location created by Android
Reading notes of Clickhouse principle analysis and Application Practice (7)
系统测试的类型有哪些,我给你介绍
Remote Sensing投稿经验分享
谈谈 SAP 系统的权限管控和事务记录功能的实现
第七章 行为级建模
Usage of hydraulic rotary joint
In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
随机推荐
Ml self realization / linear regression / multivariable
List of top ten domestic industrial 3D visual guidance enterprises in 2022
Keras深度学习实战——基于Inception v3实现性别分类
Nmap tool introduction and common commands
Beaucoup d'enfants ne savent pas grand - chose sur le principe sous - jacent du cadre orm, non, ice River vous emmène 10 minutes à la main "un cadre orm minimaliste" (collectionnez - le maintenant)
SQLite3 data storage location created by Android
Keras' deep learning practice -- gender classification based on inception V3
PHP 计算个人所得税
What are the types of system tests? Let me introduce them to you
XXL job of distributed timed tasks
保姆级教程:Azkaban执行jar包(带测试样例及结果)
Why does the updated DNS record not take effect?
云原生应用开发之 gRPC 入门
The function of carbon brush slip ring in generator
Introduction to ADB tools
Mouse event - event object
Kwai applet guaranteed payment PHP source code packaging
adb工具介绍
body有8px的神秘边距
Introduction to Microsoft ad super Foundation