📌 📄 内容摘要:
魔法师 (@Constanline) 在 Leetcode每日一题 —— 865. 具有所有最深节点的最小子树 中发帖
865. 具有所有最深节点的最小子树
思路
包含所有最深节点,那么意味着要满足以下条件
1、左子树深度=右子树深度
2、总深度最大
3、同深度下层数最小
依照规则dfs即可。
代码
class Solution {
int minLevel;
int maxDeep;
TreeNode ans;
public TreeNode subtreeWithAllDeepest(TreeNode root) {
ans = root;
maxDeep = -1;
dfs(root, 0);
return ans;
}
private int dfs(TreeNode node, int level) {
if (node == null) return ...
────────── 链接信息 ──────────
🔗 论坛链接: linux.do
📎 访问地址: https://linux.do/t/topic/1421737/1
─────────────────────────────
📢 来源:LINUX DO