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

边栏推荐
- 发现值守设备被攻击后分析思路
- 【错误】加载h5权重出错AttributeError: ‘str‘ object has no attribute ‘decode‘
- 快手小程序担保支付php源码封装
- 数据链路层及网络层协议要点
- Cross modal semantic association alignment retrieval - image text matching
- Matlab r2021b installing libsvm
- Redux usage
- Can you write the software test questions?
- Reading notes of Clickhouse principle analysis and Application Practice (7)
- 谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
猜你喜欢

很多小夥伴不太了解ORM框架的底層原理,這不,冰河帶你10分鐘手擼一個極簡版ORM框架(趕快收藏吧)

为什么更新了 DNS 记录不生效?

ANSI / nema- mw- 1000-2020 magnetic iron wire standard Latest original

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)

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

神经网络与深度学习-5- 感知机-PyTorch
![[target tracking] |dimp: learning discriminative model prediction for tracking](/img/72/d151fe0eb0a92e8c6931e6c50dad0f.png)
[target tracking] |dimp: learning discriminative model prediction for tracking

burpsuite

Many friends don't know the underlying principle of ORM framework very well. No, glacier will take you 10 minutes to hand roll a minimalist ORM framework (collect it quickly)

Remote Sensing投稿经验分享
随机推荐
Exit of processes and threads
Why does the updated DNS record not take effect?
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!!!
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
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!!!
PHP to get information such as audio duration
如何用Diffusion models做interpolation插值任务?——原理解析和代码实战
nmap工具介紹及常用命令
云原生应用开发之 gRPC 入门
进程和线程的退出
Redismission source code analysis
Codeforces Round #643 (Div. 2)——B. Young Explorers
Urban land use distribution data / urban functional zoning distribution data / urban POI points of interest / vegetation type distribution
Neural network and deep learning-5-perceptron-pytorch
剑指 Offer II 041. 滑动窗口的平均值
The function of carbon brush slip ring in generator
分布式定时任务之XXL-JOB
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
Summary of log feature selection (based on Tianchi competition)
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run