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

边栏推荐
- node js 保持长连接
- #797div3 A---C
- metasploit
- adb工具介绍
- What are the types of system tests? Let me introduce them to you
- Cross modal semantic association alignment retrieval - image text matching
- 阿南的判断
- 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!!!
- ClickHouse原理解析与应用实践》读书笔记(8)
- 云原生应用开发之 gRPC 入门
猜你喜欢

Keras' deep learning practice -- gender classification based on inception V3

JVM memory and garbage collection-3-runtime data area / heap area

Chapter 7 behavior level modeling

Applet running under the framework of fluent 3.0

Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)

云原生应用开发之 gRPC 入门

Optimization of ecological | Lake Warehouse Integration: gbase 8A MPP + xeos

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

Exit of processes and threads

PB9.0 insert OLE control error repair tool
随机推荐
Sword finger offer II 041 Average value of sliding window
Cross modal semantic association alignment retrieval - image text matching
第七章 行为级建模
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
数据链路层及网络层协议要点
[target tracking] |dimp: learning discriminative model prediction for tracking
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
cv2-drawline
Version 2.0 of tapdata, the open source live data platform, has been released
The function of carbon brush slip ring in generator
I don't know. The real interest rate of Huabai installment is so high
metasploit
How to make enterprise recruitment QR code?
pb9.0 insert ole control 错误的修复工具
Redisson分布式锁解锁异常
Remote sensing contribution experience sharing
关于TXE和TC标志位的小知识
MySQL查询为什么没走索引?这篇文章带你全面解析
Remote Sensing投稿經驗分享