当前位置:网站首页>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);
}
}
}

边栏推荐
- 日志特征选择汇总(基于天池比赛)
- 很多小夥伴不太了解ORM框架的底層原理,這不,冰河帶你10分鐘手擼一個極簡版ORM框架(趕快收藏吧)
- The numerical value of the number of figures thought of by the real-time update of the ranking list
- Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
- How to make the conductive slip ring signal better
- Sword finger offer II 041 Average value of sliding window
- Codeforces Round #649 (Div. 2)——A. XXXXX
- 批次管控如何实现?MES系统给您答案
- 给刚入门或者准备转行网络工程师的朋友一些建议
- Neural network and deep learning-5-perceptron-pytorch
猜你喜欢

Remote Sensing投稿经验分享

Why does the updated DNS record not take effect?

关于TXE和TC标志位的小知识

【目标跟踪】|atom
软件测试笔试题你会吗?

Reading notes of Clickhouse principle analysis and Application Practice (7)

Why did MySQL query not go to the index? This article will give you a comprehensive analysis

How to make the conductive slip ring signal better

Ml self realization / logistic regression / binary classification
![[target tracking] |atom](/img/33/529b483a0a848e0e4263ba24462d5c.png)
[target tracking] |atom
随机推荐
Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
如何制作企业招聘二维码?
如何用Diffusion models做interpolation插值任务?——原理解析和代码实战
C语言-Cmake-CMakeLists.txt教程
cv2读取视频-并保存图像或视频
The foreach map in JS cannot jump out of the loop problem and whether foreach will modify the original array
Codeforces Round #633 (Div. 2) B. Sorted Adjacent Differences
Mysql database (2)
MySQL数据库(2)
Chapter 7 behavior level modeling
Sword finger offer II 041 Average value of sliding window
Matlab r2021b installing libsvm
pb9.0 insert ole control 错误的修复工具
Sum of submatrix
Summary of log feature selection (based on Tianchi competition)
【目标跟踪】|DiMP: Learning Discriminative Model Prediction for Tracking
Application of slip ring in direct drive motor rotor
C语言-模块化-Clion(静态库,动态库)使用
第七章 行为级建模
2022国内十大工业级三维视觉引导企业一览