Return the smallest subtree such that it contains all the deepest nodes
in the original tree.
A node is called the deepest
if it has the largest depth possible among any node in the entire tree.
The subtree
of a node is tree consisting of that node, plus the set of all descendants of that node.
Note: This question is the same as 1123: https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]
Explanation: We return the node with value 2, colored in yellow in the diagram.
The nodes coloured in blue are the deepest nodes of the tree.
Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is the smallest subtree among them, so we return it.
Example 2:
Input: root = [1]
Output: [1]
Explanation: The root is the deepest node in the tree.
Example 3:
Input: root = [0,1,3,null,2]
Output: [2]
Explanation: The deepest node in the tree is 2, the valid subtrees are the subtrees of nodes 2, 1 and 0 but the subtree of node 2 is the smallest.
Constraints:
-
The number of nodes in the tree will be in the range [ 1 , 500 ] [1, 500] [1,500].
-
0 < = N o d e . v a l < = 500 0 <= Node.val <= 500 0<=Node.val<=500
-
The values of the nodes in the tree are unique.
受LeetCode - Medium - 236. Lowest Common Ancestor of a Binary Tree启发。
方法一:自己写的,用后序遍历模式。
方法二:别人写的,用后序遍历模式。
import com.lun.util.BinaryTree.TreeNode;
import com.lun.util.Pair;
public class SmallestSubtreeWithAllTheDeepestNodes {
//方法一:自己写的,用后序遍历模式
public TreeNode subtreeWithAllDeepest(TreeNode root) {
Object[] result = subtreeWithAllDeepest(root, 0);
return result != null ? (TreeNode)result[0] : null;
}
private Object[] subtreeWithAllDeepest(TreeNode node, int depth) {
if(node == null) return null;
depth++;
Object[] leftResult = subtreeWithAllDeepest(node.left, depth);
Object[] rightResult = subtreeWithAllDeepest(node.right, depth);
if(leftResult == null && rightResult == null) {//叶子节点
return new Object[] {node, depth};
}else if(leftResult != null && rightResult == null){
return leftResult;
}else if(leftResult == null && rightResult != null) {
return rightResult;
}else {
int leftDepth = (int)leftResult[1];
int rightDepth = (int)rightResult[1];
if(leftDepth > rightDepth) {
return leftResult;
}else if(leftDepth < rightDepth) {
return rightResult;
}else {
leftResult[0] = node;
return leftResult;
}
}
}
//方法二:别人写的,用后序遍历模式
public TreeNode subtreeWithAllDeepest2(TreeNode root) {
return deep(root).getSecond();
}
public Pair<Integer, TreeNode> deep(TreeNode root) {
if (root == null) return new Pair<>(0, null);
Pair<Integer, TreeNode> l = deep(root.left), r = deep(root.right);
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注前端)
最后
CodeChina开源项目:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
给大家分享一些关于HTML的面试题。
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算
//bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算