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

边栏推荐
猜你喜欢

Get familiar with XML parsing quickly

谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题

喜欢测特曼的阿洛

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

COMSOL --- construction of micro resistance beam model --- final temperature distribution and deformation --- addition of materials

谈谈 SAP 系统的权限管控和事务记录功能的实现

PB9.0 insert OLE control error repair tool

Matlab r2021b installing libsvm

XXL job of distributed timed tasks
软件测试笔试题你会吗?
随机推荐
nmap工具介紹及常用命令
Is it necessary for project managers to take NPDP? I'll tell you the answer
关于TXE和TC标志位的小知识
鱼和虾走的路
Redisson distributed lock unlocking exception
PHP 计算个人所得税
I don't know. The real interest rate of Huabai installment is so high
【SolidWorks】修改工程图格式
C language - modularization -clion (static library, dynamic library) use
Redismission source code analysis
电路如图,R1=2kΩ,R2=2kΩ,R3=4kΩ,Rf=4kΩ。求输出与输入关系表达式。
How mysql/mariadb generates core files
[error] error loading H5 weight attributeerror: 'STR' object has no attribute 'decode‘
Codeforces Round #643 (Div. 2)——B. Young Explorers
【目标跟踪】|atom
Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
COMSOL --- construction of micro resistance beam model --- final temperature distribution and deformation --- addition of materials
Version 2.0 of tapdata, the open source live data platform, has been released
Analysis ideas after discovering that the on duty equipment is attacked
阿锅鱼的大度