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

边栏推荐
- Sword finger offer II 041 Average value of sliding window
- Reading notes of Clickhouse principle analysis and Application Practice (7)
- 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)
- 咋吃都不胖的朋友,Nature告诉你原因:是基因突变了
- If time is a river
- leetcode 873. Length of Longest Fibonacci Subsequence | 873. 最长的斐波那契子序列的长度
- The generosity of a pot fish
- C语言-Cmake-CMakeLists.txt教程
- JVM memory and garbage collection-3-runtime data area / heap area
- Introduction à l'outil nmap et aux commandes communes
猜你喜欢

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

数据链路层及网络层协议要点

Why does the updated DNS record not take effect?

leetcode 866. Prime Palindrome | 866. 回文素数

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

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

Give some suggestions to friends who are just getting started or preparing to change careers as network engineers

系统测试的类型有哪些,我给你介绍

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

Sword finger offer II 041 Average value of sliding window
随机推荐
Redission源码解析
Ml self realization / linear regression / multivariable
Analysis ideas after discovering that the on duty equipment is attacked
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
分布式定时任务之XXL-JOB
List of top ten domestic industrial 3D visual guidance enterprises in 2022
关于TXE和TC标志位的小知识
Redisson分布式锁解锁异常
Wechat applet uniapp page cannot jump: "navigateto:fail can not navigateto a tabbar page“
电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
Matlab r2021b installing libsvm
Ml backward propagation
Exit of processes and threads
The numerical value of the number of figures thought of by the real-time update of the ranking list
Partage d'expériences de contribution à distance
JVM memory and garbage collection -4-string
Keras深度学习实战——基于Inception v3实现性别分类
mysql/mariadb怎样生成core文件
#797div3 A---C
cv2-drawline