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

边栏推荐
- 如何用Diffusion models做interpolation插值任务?——原理解析和代码实战
- 保姆级教程:Azkaban执行jar包(带测试样例及结果)
- How mysql/mariadb generates core files
- 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!!!
- 进程和线程的退出
- QML fonts use pixelsize to adapt to the interface
- Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
- Keras' deep learning practice -- gender classification based on inception V3
- Sword finger offer II 041 Average value of sliding window
- If time is a river
猜你喜欢

《ClickHouse原理解析与应用实践》读书笔记(7)

Nanny level tutorial: Azkaban executes jar package (with test samples and results)

XXL job of distributed timed tasks

Version 2.0 of tapdata, the open source live data platform, has been released

【SolidWorks】修改工程图格式

咋吃都不胖的朋友,Nature告诉你原因:是基因突变了

给刚入门或者准备转行网络工程师的朋友一些建议

Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布

PB9.0 insert OLE control error repair tool

很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)
随机推荐
metasploit
The foreach map in JS cannot jump out of the loop problem and whether foreach will modify the original array
Node JS maintains a long connection
【目标跟踪】|atom
Remote sensing contribution experience sharing
Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
#797div3 A---C
node js 保持长连接
Ml self realization / logistic regression / binary classification
[target tracking] |atom
Codeforces Round #643 (Div. 2)——B. Young Explorers
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
很多小夥伴不太了解ORM框架的底層原理,這不,冰河帶你10分鐘手擼一個極簡版ORM框架(趕快收藏吧)
如何制作企业招聘二维码?
How mysql/mariadb generates core files
为什么更新了 DNS 记录不生效?
ClickHouse原理解析与应用实践》读书笔记(8)
Graphic network: uncover the principle behind TCP's four waves, combined with the example of boyfriend and girlfriend breaking up, which is easy to understand
The circuit is shown in the figure, r1=2k Ω, r2=2k Ω, r3=4k Ω, rf=4k Ω. Find the expression of the relationship between output and input.
Why did MySQL query not go to the index? This article will give you a comprehensive analysis