当前位置:网站首页>leetcode 865. Smallest Subtree with all the Deepest Nodes | 865. The smallest subtree with all the deepest nodes (BFs of the tree, parent reverse index map)
leetcode 865. Smallest Subtree with all the Deepest Nodes | 865. The smallest subtree with all the deepest nodes (BFs of the tree, parent reverse index map)
2022-07-08 02:03:00 【Cold spring HQ】
subject
https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/
Answer key
/** * 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) {
// Sequence traversal , Record the deepest layer
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;
}
// Create a tree reverse index table
HashMap<TreeNode, TreeNode> parentMap = new HashMap<>();
dfs(root, parentMap);
// Look up from every node in the deepest layer parent, Passing by , Node passing times count++, Find the earliest count==n The ancestors of the
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);
}
}
}

边栏推荐
- uniapp一键复制功能效果demo(整理)
- Redission源码解析
- 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!!!
- 【错误】加载h5权重出错AttributeError: ‘str‘ object has no attribute ‘decode‘
- 快手小程序担保支付php源码封装
- VIM string substitution
- Give some suggestions to friends who are just getting started or preparing to change careers as network engineers
- The method of using thread in PowerBuilder
- Remote Sensing投稿經驗分享
- Partage d'expériences de contribution à distance
猜你喜欢

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

Usage of hydraulic rotary joint

【SolidWorks】修改工程图格式

MySQL查询为什么没走索引?这篇文章带你全面解析

JVM memory and garbage collection-3-object instantiation and memory layout

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

adb工具介绍

Nacos microservice gateway component +swagger2 interface generation
![[recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning](/img/48/3366df75c397269574e9666fcd02ec.jpg)
[recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning
![[target tracking] |atom](/img/33/529b483a0a848e0e4263ba24462d5c.png)
[target tracking] |atom
随机推荐
burpsuite
leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
The body has a mysterious margin of 8px
XXL job of distributed timed tasks
PB9.0 insert OLE control error repair tool
Optimization of ecological | Lake Warehouse Integration: gbase 8A MPP + xeos
鼠标事件-事件对象
力扣5_876. 链表的中间结点
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
cv2-drawline
Height of life
[target tracking] |dimp: learning discriminative model prediction for tracking
静态路由配置全面详解,静态路由快速入门指南
C language -cmake cmakelists Txt tutorial
分布式定时任务之XXL-JOB
JVM memory and garbage collection-3-runtime data area / heap area
See how names are added to namespace STD from cmath file
C语言-Cmake-CMakeLists.txt教程
电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
Matlab r2021b installing libsvm